Returning a single column of Pandas dataframe *as a dataframe* after filtering on another column

Question:

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 for me is that df_filter is returned as a Series (I need the return to be a data frame).

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?

Asked By: equanimity

||

Answers:

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
Answered By: user17242583

We can also use filter() as it returns same type as input object

df_filter = df[the_filter].filter(['food'])
type(df_filter)
<class 'pandas.core.frame.DataFrame'>
Answered By: asquare
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.