Python 累计计算行(单位:df)
我有一只大熊猫,如下所示Python 累计计算行(单位:df),python,pandas,Python,Pandas,我有一只大熊猫,如下所示 Country category brand quarter device countA CountB percentageA/B XXX A1 A2 Q2 PC 12 12 100 XXX A1 A2 Q2 Tablet 2 4 50 YYY A4 A5 Q4 PC 50
Country category brand quarter device countA CountB percentageA/B
XXX A1 A2 Q2 PC 12 12 100
XXX A1 A2 Q2 Tablet 2 4 50
YYY A4 A5 Q4 PC 50 50 100
YYY A4 A5 Q4 Tablet 10 10 100
我需要在数据中添加一行,这是以上两个数据点的总和
Country category brand quarter device countA CountB percentage(A/B)
XXX A1 A2 Q2 PC 12 12 100 %
XXX A1 A2 Q2 Tablet 2 4 50 %
**XXX A1 A2 Q2 PC + Tablet 14 16 87.5%**
YYY A4 A5 Q4 PC 50 50 100
YYY A4 A5 Q4 Tablet 10 12 83%
**YYY A4 A5 Q4 PC+Tablet 60 62 96.7%**
请找出d的结构
因此,理想情况下,该类别中只有一个设备的品牌很少
Country category brand quarter device
XXX A1 A2 Q2 Tablet +PC
A4 A5 Q2 Tablet+PC
A9 A10 Q2 PC
A11 Q1 PC
印刷品(d类)
使用
groupby
merge
和concat
此外,您还从未提到如何计算百分比a/B
# groupby and apply with join to get devices
d = df.groupby(['Country','category','brand','quarter'])['device'].apply('+'.join)
# groupby with sum then merge the two groups together with reset_index
new = df.groupby(['Country','category','brand','quarter']).sum().merge(d, left_index=True, right_index=True).reset_index()
# concat original df with new
pd.concat([df,new], sort=False)
Country category brand quarter device countA CountB percentageA/B
0 XXX A1 A2 Q2 PC 12 12 100
1 XXX A1 A2 Q2 Tablet 2 4 50
2 YYY A4 A5 Q4 PC 50 50 100
3 YYY A4 A5 Q4 Tablet 10 10 100
0 XXX A1 A2 Q2 PC+Tablet 14 16 150
1 YYY A4 A5 Q4 PC+Tablet 60 60 200
或者您可以尝试:
# groupby and apply with join to get devices
d = df.groupby(['Country','category','brand','quarter'])['device'].apply('+'.join).to_frame().reset_index()
# groupby with sum then merge the two groups together with reset_index
new = df.groupby(['Country','category','brand','quarter'], as_index=False).sum().merge(d, on=['Country','category','brand','quarter'])
# concat original df with new
final_df = pd.concat([df,new], sort=False)
final_df['percentageA/B'] = final_df['countA'] / final_df['CountB'] * 100
我们需要更多的信息。你试过什么?您是如何计算百分比A/B的?查看df.groupby('Country').sum()我尝试了完全相同的结构,但它以不同于所需df.groupby(['Country','category','brand','quarter])的格式提供了输出。sum()合并会产生一个错误:无法将DataFrame与@NehaSharma类型的实例合并您正在使用的pandas的哪个版本,因为它在.24.1中运行良好;不过,请尝试将
d
改为d。改为\u frame()
谢谢您的耐心,克里斯。我用的是熊猫:0.23.4。上述建议仍然会产生相同的错误。您可以执行打印(d.head(10))
和打印(type(d))
并将结果粘贴到您的问题中,这样我就可以看到它的外观并添加您正在尝试的代码。这里来自文档“在版本0.24.0中添加了对合并命名系列对象的支持”
看来您需要升级pandas才能使上述代码正常工作。如果您有兴趣升级熊猫,请阅读,以便了解它将如何影响以前的项目。