Amit Pathak
Amit Pathak

Reputation: 1367

Incrementing a cell value in pandas dataframe based on multiple column conditions

I am trying to increment the integer value present in column_3 based on checks in column_1 and column_2.

  | column_1 | column_2 | column_3
0 |    a     |    x     |    1 
1 |    b     |    y     |    1 
2 |    c     |    z     |    1 

I want to increment the value present in column_3 by 1 if column_1 = a and column_2 = x.

I tried the following approach but it does not work -

df = pd.DataFrame({
    "column_1": ['a', 'b', 'c'], 
    "column_2": ['x', 'y', 'z'],
    "column_3": [1,1,1]
})

df[(df['column_1']=='a') & (df['column_2']=='x')]['column_3'] += 1

Checking the df output after running the above code gives the same dataframe.

After Jupyter showed the warning I made use of the .loc but still it does not work.

df.loc[df.column_1.isin(['a']) & df.column_2.isin(['x'])]['column_3'].iat[0] += 1

How exactly do we achieve this ?

Upvotes: 1

Views: 1304

Answers (1)

jezrael
jezrael

Reputation: 862581

Using DataFrame.loc is correct way, but is necessary also remove ][ and iat:

df.loc[(df['column_1']=='a') & (df['column_2']=='x'), 'column_3'] += 1

print (df)
  column_1 column_2  column_3
0        a        x         2
1        b        y         1
2        c        z         1

Another solution with numpy.where:

df['column_3'] += np.where((df['column_1']=='a') & (df['column_2']=='x'), 1, 0)

print (df)
  column_1 column_2  column_3
0        a        x         2
1        b        y         1
2        c        z         1

Upvotes: 3

Related Questions