Python Forum

Full Version: How to replace matching rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
You can do this e.g., as follows:

df.iloc[:, 1][df.iloc[:, 1].str.contains('pass', case=False)] = 0
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