1

I have a data set like this.

df = pd.DataFrame({"A" :[1,1,3,4], "B": [1,3,2,2]})

I want to create a new column which is C with type 1 if A = 1 & B =(1,3) I used .loc and my code is

df.loc[(df['A'] == 1)&(df['B'] == 1), 'C'] = 'type 1'
df.loc[(df['A'] == 1)&(df['B'] == 3), 'C'] = 'type 1'

The above is working, but when I use

df.loc[(df['A'] == 1)&(df['B'] == (1,3)), 'C'] = 'type 1'

Nothing happens, it doesn't show error and column is also not updated.

The expected output is

A   B   C
1   1   type 1
1   3   type 1
3   2   Nan
4   2   Nan

Is there any other way?

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

3

Use numpy.where:

In [1517]: import numpy as np
In [1518]: df['C'] = np.where(df.A.eq(1) & df.B.isin([1,3]), 'type 1', np.nan)

In [1519]: df
Out[1519]: 
   A  B       C
0  1  1  type 1
1  1  3  type 1
2  3  2     nan
3  4  2     nan
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

The other ways may be to try using .eval similar to answer here:

df.loc[df.eval('A ==1 and B in [1,3]'), 'C']= 'type 1'

In case if you want to fix the code you have may be you can try separating with |:

df.loc[(df['A'] == 1)&((df['B'] ==1) | (df['B'] ==3)), 'C'] = 'type 1'
niraj
  • 17,498
  • 4
  • 33
  • 48
1

Here is a possible solution that does not use any library but pandas:

df['C'] = pd.Series(index=range(len(df)), dtype='float')
df['C'][df['A'] == 1 & df['B'].isin((1, 3))] = 'type 1'
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50