0

I have a large data frame and I need to loop through and change one row value based on the same row value of another column. I have read this post Pandas/Python: Set value of one column based on value in another column and this blog post https://towardsdatascience.com/a-really-simple-way-to-edit-row-by-row-in-a-pandas-dataframe-75d339cbd313 but this method isn't working for me. What I think is going wrong is that the column I am indexing from (label2) contains null values in some of the rows.

for index in df.index:
 if df.loc[index,'label2']=='Some label to match':df.loc[index,'label1'] = 'label to change'

Is there a way to get around this in pandas, e.g., using df.notnull() within the loop? I have tried the following but it doesn't work:

df['label2'].notnull(df.loc[index,'label2']=='Some label to match':df.loc[index,'label1'] = 'label to change')

Thanks in advance!

0 Answers0