Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas : update value if condition in 3 columns are met

Tags:

python

pandas

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 
like image 660
Eduardo Oliveira Avatar asked Jan 21 '14 15:01

Eduardo Oliveira


People also ask

How replace column values in Pandas based on multiple conditions?

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.


1 Answers

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' 
like image 58
Praveen Avatar answered Sep 23 '22 22:09

Praveen