I've seen a lot of posts similar but none seem to answer this question:
I have a data frame with multiple columns. Lets say A, B and C
I want to change column A's value based on conditions on A, B and C
I've got this so far but not working.
df=df.loc[(df['A']=='Harry')
& (df['B']=='George')
& (df['C']>'2019'),'A']=='Matt'
So if A is equal to Harry, and B is equal to George and C is greater than 2019, then change A to Matt
Anyone see what I've done wrong?
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
To do a conditional update depending on whether the current value of a column matches the condition, you can add a WHERE clause which specifies this. The database will first find rows which match the WHERE clause and then only perform updates on those rows.
You are really close, assign value Matt
to filtered A
by boolean masks:
df.loc[(df['A']=='Harry') & (df['B']=='George') & (df['C']>'2019'),'A'] = 'Matt'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With