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 loop through 2 columns in a dataframe and replace the value in another column with something else?

So I need to find replace the value for annual_data[‘countyfips’] from ‘NA’ to 5097 where annual_data [‘BUYER_STATE’] == ‘AR’ and annual_data[‘BUYER_COUNTY’] == ‘MONTGOMERY’.

Below is my code:

annual_data = annual_data[['BUYER_COUNTY', 'BUYER_STATE', 'year', 'DOSAGE_UNIT', 'countyfips']]
annual_data

for index, values in annual_data.iterrows():
    if (values['BUYER_STATE'] == 'AR') and (values['BUYER_COUNTY'] == 'MONTGOMERY'):
        annual_data.at[index, 'countyfips'] == 5097
print(annual_data)

However, annual_data[‘countyfips’] still equals ‘NA’.

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

>Solution :

Change the == to = here:

From

    annual_data.at[index, 'countyfips'] == 5097

to

    annual_data.at[index, 'countyfips'] = 5097
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