Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to update dataframe cell value based on values in other columns?

I have a pandas dataframe (called removedCols) of ~2000 rows, and I am trying to populate certain columns in my dataframe by using values in corresponding cells. An exerpt of the original dataframe is as such:

 A      B      C      D     labels
 0      0      0      0     ['D', 'C']
 0      0      0      0     []
 0      0      0      0     ['A','B','D']
 0      0      0      0     ['D']

My goal is to replace the values for the corresponding columns, in the labels column. Such that we get,

 A      B      C      D     labels
 0      0      1      1     ['D', 'C']
 0      0      0      0     []
 1      1      0      1     ['A','B','D']
 0      0      0      1     ['D']

I have tried many different solutions, such as first extracting labels to a list, and iterating over that, or iterating over the indexes of the dataframe.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for i in removedCols.index:
     for value in removedCols.iloc[i]['labels']:
          removedCols.at[i, value] = 1

However, these solutions seem to provide random combinations of 0’s and 1’s – and do not exactly match with what is given in labels column.

UPDATE: Double check your indexes.

>Solution :

Use Series.str.get_dummies:

import ast
#if necessary
#df['labels'] = df['labels'].apply(ast.literal_eval)

df.update(df['labels'].str.join('|').str.get_dummies())
print (df)

   A  B  C  D     labels
0  0  0  1  1     [D, C]
1  0  0  0  0         []
2  1  1  0  1  [A, B, D]
3  0  0  0  1        [D]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading