Reputation: 303
df is a pandas dataframe.
this gives me my young subjects:
df_young = df[df['Age']<40]
this gives me my old subjects:
df_old = df[df['Age']>60]
Now I want to get my middleaged subjects with something like (invalid syntax):
df_middleage = df[df['Age']< 40 and < 60]
Does anyone know how to do that efficiently? Thanks.
Upvotes: 2
Views: 43
Reputation: 139172
You can use &
:
df_middleage = df[(df['Age'] > 40) & (df['Age'] < 60)]
See the docs here: http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing
Upvotes: 3