Let's assume we have the following data frame df
:
df = pd.DataFrame({'food' : ['spam', 'ham', 'eggs'],
'price' : [10, 20, 30],
'inventory' : ['normal', 'high', 'low']
I want to filter df
and return only the elements of the food
column with a price greater than 15. To do so, I use:
the_filter = df['price'] > 15
df_filter = df[the_filter]['food']
df_filter
1 ham
2 eggs
Name: food, dtype: object
The problem is that df_filter
is returned as a Series.
type(df_filter)
pandas.core.series.Series
We can use .loc
and get back a dataframe object:
df.loc[:, ['food']]
But, how do we filter by price
?
Thanks!
CodePudding user response:
Specify the condition(s) and the column(s) to return in on go with .loc
:
df_filter = df.loc[df['price'] > 15, ['food']]
Output:
>>> df_filter
food
1 ham
2 eggs
>>> type(df_filter)
pandas.core.frame.DataFrame
CodePudding user response:
Change you code with chain
df_filter = df[the_filter][['food']]