Python Forum
How to replace matching rows - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to replace matching rows (/thread-19718.html)



How to replace matching rows - SriMekala - Jul-11-2019

Hi,
I have below DataFrame, I want to replace if row value contains 'pass' with '0'.

import pandas as pd

df=pd.DataFrame({'Name':['NHAY', 'HAY','UTY','UIK', 'JUI','YTH'],'Status':['Pass','Running','pass(warning 0)','pass','fail','passed']})
df.loc[df['Status'].str.contains('pass', case=False), 'Status'] = 0
but if in case I want to column number (like here in this case 1), how to do it.


RE: How to replace matching rows - scidam - Jul-12-2019

You can do this e.g., as follows:

df.iloc[:, 1][df.iloc[:, 1].str.contains('pass', case=False)] = 0



RE: How to replace matching rows - SriMekala - Jul-13-2019

I want to add a column and apply the above condition, I use below code but all rows fill with zeros.

df['Add']=df.loc[df['Status'].str.contains('pass', case=False), 'Status'] = 0