![](/img/trans.png)
[英]How to get the sum of values from one column with the conditional of another column
[英]How to sum values of one column and group them by another column
我有一個這樣的數據框:
df
time type qty
12:00 A 5
13:00 A 1
14:00 B 9
我需要對qty
的值求和並按type
它們分組。 我就是這樣做的,但似乎不起作用,因為我不知道如何添加qty
。
keys = df['type'].unique()
summary = pd.DataFrame()
for k in keys:
summary[k] = df[df['type']==k].sum()
為了確保您總結了您想要的列:
df.groupby(by=['type'])['qty'].sum()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.