python - DataFrame in list boolean? -


i can select subset using boolean indexing such:

df.loc[df['my column'] == 1] 

what i'd replace == 1 list, [1, 2].

something like:

df.loc[df['my column'] in [1,2]] 

this equivalent of long or statement.

how do nicely pandas?

(alternative, non-boolean answers too).

use isin test membership in list of values:

df.loc[df['my column'].isin([1,2])] 

example:

in [18]: df = pd.dataframe({'a':np.random.randn(5), 'b':np.arange(5)}) df  out[18]:            b 0 -0.118930  0 1 -0.129362  1 2  0.822227  2 3  1.781672  3 4 -0.127322  4  in [19]: df.loc[df['b'].isin([2,4])]  out[19]:            b 2  0.822227  2 4 -0.127322  4 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -