Reputation: 4892
I have a pandas DataFrame that looks like this:
date sku qty
0 2015-10-30 ABC 1
1 2015-10-30 DEF 1
2 2015-10-30 ABC 2
3 2015-10-31 DEF 1
4 2015-10-31 ABC 1
... ... ... ...
How can extract all of the data for a particular sku
and sum up the qty
by date. For example, the ABC
SKU?
2015-10-30 3
2015-10-31 1
... ...
The closest I've gotten is a hierarchal grouping with sales.groupby(['date', 'sku']).sum()
.
Upvotes: 0
Views: 29
Reputation: 150745
If you will work with all (or several) sku
, then:
agg_df = df.groupby(['sku','date']).qty.sum()
# extract some sku data
agg_df.loc['ABC']
Output:
date
2015-10-30 3
2015-10-31 1
Name: qty, dtype: int64
If you only care for ABC
particularly, then it's better to filter it first
df[df['sku'].eq('ABC')].groupby('date')['qty'].sum()
The output would be the same as above.
Upvotes: 2