python - How to select rows in pandas based on list of values -
i'm trying find out way how can select rows in pandas dataframe based values in list. example
df = pd.dataframe(np.arange(6).reshape(3,2), columns=['a','b']) b 0 0 1 1 2 3 2 4 5 i know can select row, e.g.
df[df.a==0] will select me row a=0. want select multiple rows values in list, e.g. in [0,2]. tried
df[df.a in [0,2]] df[list(df.a)==[0,2]] but nothing works. in r language can provide %in% operator. in python syntax can use in [0,2], etc. how can select subset of rows in pandas in case? thanks, valentin.
pd.isin() select multiple values:
>>> df[df.a.isin([0,2])] b 0 0 1 1 2 3
Comments
Post a Comment