0

Suppose I have a DF:

DF:
Inp | M |  N | P 
 GF | 1 | 12 |
 GF | 1 | 12 |
 GF | 1 | 13 | 

and I want to place a value in any rows where Inp = GF and M = 1 and N = 12 to get an output like this:

DF2:
Inp | M |  N | P 
 GF | 1 | 12 | X
 GF | 1 | 12 | X
 GF | 1 | 13 | 

I know I can select by multiple attributes using .loc like this:

df.loc[(DF['Inp'] = 'GF') & (DF['M'] = 1) & (DF['N'] = 12)]

But I'm not sure how to then place a value in column P. Maybe I'm not on the right track.

AGH_TORN
  • 813
  • 17
  • 33
  • 1
    See piR's solution in the duplicate, specifically point **3. boolean arrays** which is exactly this kind of selection – ALollz Jan 17 '19 at 16:07

1 Answers1

2

I guess you are on the right track. Just add the column and asign your value:

df.loc[(DF['Inp'] = 'GF') & (DF['M'] = 1) & (DF['N'] = 12), "P"] = X
Jones1220
  • 786
  • 2
  • 11
  • 22