我有一个看起来像这样的数据框:
prod_code month items cost
0 040201060AAAIAI 2016-05-01 5 572.20
1 040201060AAAKAK 2016-05-01 164 14805.19
2 040201060AAALAL 2016-05-01 13465 14486.07
我想首先对的前四个字符进行分组prod_code
,然后对2016年1月至2月每个组的总成本进行求和,然后将其与2016年3月至4月的总成本进行比较,然后找出百分比增长幅度最大的组。两个时间段。
最好的方法是什么?
到目前为止,这是我的代码:
d = { 'prod_code': ['040201060AAAIAI', '040201060AAAIAJ', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040201060AAAIAI', '040301060AAAKAG', '040301060AAAKAK', '040301060AAAKAK', '040301060AAAKAX', '040301060AAAKAK', '040301060AAAKAK'], 'month': ['2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01', '2016-01-01', '2016-02-01', '2016-03-01'], 'cost': [43, 45, 46, 41, 48, 59, 8, 9, 10, 12, 15, 13] }
df = pd.DataFrame.from_dict(d)
df['para'] = df.prod_code.str[:4]
df_para = df.groupby(['para', 'month']).sum()
这使我df_para
看起来像这样:
cost
para month
0402 2016-01-01 84
2016-02-01 93
2016-03-01 105
0403 2016-01-01 20
2016-02-01 24
2016-03-01 23
现在,我需要计算1月至2月和4月至3月每组的总和,然后计算这两组之间的差异,最后按这两组之间的差异进行排序。做这个的最好方式是什么?
您可以根据月份是Jan-Feb
还是月份创建月份组变量Mar-Apr
,然后按代码和月份组变量分组,汇总成本并计算差额:
import numpy as np
import pandas as pd
df['month_period'] = np.where(pd.to_datetime(df.month).dt.month.isin([1,2]), 1, 2)
# creation of the month group variable could be adjusted based on how you want to cut
# your time, this is a simplified example which assumes you only have data from Jan-Apr
(df.groupby([df.prod_code.str[:4], df.month_period]).sum().groupby(level = 0).pct_change()
.dropna().sort('cost', ascending=False))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句