Home > Back-end >  How to update multiple row values in pandas dataframe?
How to update multiple row values in pandas dataframe?

Time:01-10

I want to update a specific row value(appearing multiple times in a dataframe) due to its length size.

Here is my sample dataset with two columns.

metro_regional  status
Metro        Referred
Regional    Referred
Metro       Referred
Regional    Referred
Metro       Referred
Regional    <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New
Metro       <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New
Metro       <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New
Regional    <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New
Metro       <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New

From the above dataset, I want to update row value <img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New to Others

Desired output;

metro_regional  status
Metro           Referred
Regional        Referred
Metro           Referred
Regional        Referred
Metro           Referred
Regional        Others
Metro           Others
Metro           Others
Regional        Others
Metro           Others

How can I update multiple row values with a specific one?

Any help would be appreciated.

CodePudding user response:

You can just assign it

str = '<img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New'
df.loc[df['status'].eq(str),'status'] = 'other'

CodePudding user response:

you can use .loc to update specific columns where a condition holds.

other_status =  '<img src="/resource/status_icons/status-new.png" alt=" " style="height:22px; width:22px;" border="0"/> New'
df.loc[df.status == other_status, 'status'] = 'Others'
  •  Tags:  
  • Related