Skip to content
Advertisement

Substitute numbers in a list of type object pandas

I have a dataframe df looking as follows:

JavaScript

What I would like to do is to substitute into df['cited_ids'] 0 whenever the corresponding id has d=0 (i) and replace d=1 if there is at least one 0 in the list of df['cited_ids'] and the previous d was not 0 (ii). In other words, the first step (i) would result in:

JavaScript

The second step (ii) would then result in:

JavaScript

Please also notice that the dataframe comes with df['cited_ids'] being an object.

df.to_dict() gives:

JavaScript

Thank you

Advertisement

Answer

The exact logic is unclear and your output doesn’t seem to match the description, but IIUC:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement