1
    max_speed  shield  new
1   1          2       3
2   4          5       6

I got this dataframe, and I want to assign a value according to some column names. like this df.loc[df['max_speed']==1, df['shield']==2, new] = 10 then I will get the new dataframe:

    max_speed  shield  new
 1  1          2       10
 2  4          5       6

Anyone know how to do it?

Lilian417
  • 59
  • 7

2 Answers2

2

you are pretty close.

df.loc[(df.max_speed == 1) & (df.shield == 2), "new"] = 10

For further reading, see here.

sudonym
  • 3,788
  • 4
  • 36
  • 61
2

Alternative with where if you want to change the value if doesnt fullfill your conditions:

df['new'] = np.where((df['max_speed']==1) & (df['shield']==2) , 10, df['new'])
Joe
  • 12,057
  • 5
  • 39
  • 55