[英]Pandas Groupby For Missing Rows
I have a dataframe that looks like this (actual df contains million rows), consisting of data for Week 1,2 and 3. If the Qty is 0 for that week, then the rows are not present.我有一个看起来像这样的数据框(实际 df 包含百万行),由第 1,2 和第 3 周的数据组成。如果该周的数量为 0,则行不存在。 For example, Product A qty=0 for Week 2, thus there is no Week 2 row shown in the dataframe.
例如,第 2 周的产品 A 数量=0,因此数据框中没有显示第 2 周的行。
Product![]() |
Week![]() |
Qty![]() |
---|---|---|
A![]() |
1 ![]() |
10 ![]() |
A![]() |
1 ![]() |
10 ![]() |
A![]() |
3 ![]() |
10 ![]() |
B![]() |
2 ![]() |
10 ![]() |
B![]() |
2 ![]() |
10 ![]() |
I want to groupby into this dataframe showing the qty for each week including when Qty=0.我想分组到这个数据框中,显示每周的数量,包括 Qty=0 时的数量。 How to achieve this?
如何实现这一目标? Thank you.
谢谢你。
Product![]() |
Week![]() |
Qty![]() |
---|---|---|
A![]() |
1 ![]() |
20 ![]() |
A![]() |
2 ![]() |
0 ![]() |
A![]() |
3 ![]() |
10 ![]() |
B![]() |
1 ![]() |
0 ![]() |
B![]() |
2 ![]() |
20 ![]() |
B![]() |
3 ![]() |
0 ![]() |
#Groupby and sum
df1=df.groupby(['Product','Week'])['Qty'].sum().to_frame()
#reindex, multiindex
df1.reindex( pd.MultiIndex.from_product([df1.index.levels[0],
df['Week'].unique()], names=['Product', 'Week']),fill_value=0).reset_index()
Product Week Qty
0 A 1 20
1 A 3 10
2 A 2 0
3 B 1 0
4 B 3 0
5 B 2 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.