I have a dataframe like this:
In[1]: df Out[1]: A B C D 1 blue red square NaN 2 orange yellow circle NaN 3 black grey circle NaN
and I want to update column D when it meets 3 conditions. Ex:
df.ix[ np.logical_and(df.A=='blue', df.B=='red', df.C=='square'), ['D'] ] = 'succeed'
It works for the first two conditions, but it doesn't work for the third, thus:
df.ix[ np.logical_and(df.A=='blue', df.B=='red', df.C=='triangle'), ['D'] ] = 'succeed'
has exactly the same result:
In[1]: df Out[1]: A B C D 1 blue red square succeed 2 orange yellow circle NaN 3 black grey circle NaN
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.
Using:
df[ (df.A=='blue') & (df.B=='red') & (df.C=='square') ]['D'] = 'succeed'
gives the warning:
/usr/local/lib/python2.7/dist-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead
A better way of achieving this seems to be:
df.loc[(df['A'] == 'blue') & (df['B'] == 'red') & (df['C'] == 'square'),'D'] = 'M5'
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