简体   繁体   中英

Filtering dataframe in pandas based on a list of strings

I have started exploring pandas recently, I am trying to import a list of fruits from sector.py and use it as a filter to produce a table of items where only fruits within the list are displayed. I am not getting the output desired is there something wrong with my codes?

Within sector.py

Fruits=['Apple','Orange','Pineapples']

Within calculator.py

import sector
import pandas as pd

pdmart = pd.read_csv('supermarket.csv')
pdextract = pdmart.groupby('item')['price'].sum()

Fruits = pdextract[pdextract.isin(sector.Fruits)]
print Fruits

Current output:

Series([], Name: price, dtype: float64)

Desired output:

Item         Price
Apple        12.0
Orange       7.0
Pineapples   15.0

Applying isin on a GroupBy object doesn't make sense. You can use Boolean indexing on the index of your GroupBy object:

Fruits = pdextract[pdextract.index.isin(sector.Fruits)]

You can also filter on a series before your GroupBy operation:

pdextract = pdmart.loc[pdmart['item'].isin(sector.Fruits)]\
                  .groupby('item')['price'].sum()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM