1

I have a data frame as follows,

df2 = pd.DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],
                    'b' : ['x', 'y', 'y', 'x', 'y', 'x', 'x'],
                    'c' : np.random.randn(7)})

I want to select data from df2 where column 'a' equals 'two' or 'three', my code is as follows,

df2[df2['a']=='two'or df2['a']=='three']

Could anyone give me some light on why my code doesn't work?

Error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Thanks in advance!

3 Answers3

2

is that isin

slice=df2.loc[df2.a.isin(['one','two'])].copy()
slice
Out[797]: 
     a  b         c
0  one  x -0.064378
1  one  y  0.344902
2  two  y -0.080087
4  two  y  1.433515
5  one  x  1.065794
BENY
  • 317,841
  • 20
  • 164
  • 234
1

Use | instead of or

df2[(df2['a']=='two') | (df2['a']=='three')]
satomacoto
  • 11,349
  • 2
  • 16
  • 13
  • I know '|' is bitwise and or is a logic, but I am confused why here I need to use | rather than or, could you explain a little bit? Thank you! – Chelsea Yang May 14 '19 at 20:05
0

You're close but you need to do a few things, first use the | operator to specify an or statement, secondly put each condition in brackets,

This should work

df2.loc[(df2['a']=='two') | (df2['a']=='three')]

Umar.H
  • 22,559
  • 7
  • 39
  • 74