[英]Using Pandas to calculate using groupby() and sum() from a DataFrame
I am currently in the process of learning Pandas and I am stuck at an exercise.我目前正在学习 Pandas,但我被困在一个练习中。
My problem is as follows: I need to calculate the number of total purchases by a cardholder in a month of more than $25000 .我的问题如下:我需要计算一个持卡人一个月内的总购买次数超过$25000 。 For this I have been given a hint that I first must create a DataFrame that includes the total purchases by each cardholder in each month (using the
groupby()
and sum()
methods), and then use this DataFrame to do the calculation.)为此,我得到了一个提示,我首先必须创建一个 DataFrame,其中包含每个持卡人每个月的总购买量(使用
groupby()
和sum()
方法),然后使用这个 DataFrame 进行计算。)
df_3= df[['Calendar Month','Cardholder Name']].groupby(df['Amount']).sum()
df_3
Which gives the following output这给出了以下输出
Amount Calendar Month Cardholder Name
-5924.00 5 JEFFRIES, T
-5522.21 11 LAIR, M
-4800.00 11 KENT, D
-4444.23 6 LAIR, M
-4364.50 2 FOISY, J
... ... ...
20876.68 3 JEFFRIES, T
27087.50 12 COLE, J
29585.16 2 JEFFRIES, T
57510.00 1 LACEY, L
62181.77 4 LACEY, L
I think that this initial Dataframe (df_3) is right, but I don't know how the actual calculation must be done to calculate the number of total purchases by a cardholder in a month of more than $25000.我认为这个初始Dataframe(df_3)是对的,但是我不知道必须如何进行实际计算才能计算出持卡人一个月内的总购买次数超过25000美元。
here is one way to do it.这是一种方法。 you store the groupby result in a var and then filter it on your condition.
您将 groupby 结果存储在 var 中,然后根据您的条件对其进行过滤。
gb=df.groupby(['Month','Cardholder Name'])['amount'].sum().to_frame()
gb[gb['amount'] > 25000]
amount
Month Cardholder Name
1 LACEY, L 57510.00
2 JEFFRIES, T 29585.16
4 LACEY, L 62181.77
12 COLE, J 27087.50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.