我有一个数据集df,我希望通过以下类型来计算增长增长:
location size type date
ny 5 hello 10/01/2020
ny 7 ai 10/02/2020
ny 8 ai 10/03/2020
ny 6 hello 10/04/2020
ca 15 cool 10/05/2020
ca 10 name 10/06/2020
ca 5 name 10/07/2020
ca 16 cool 10/08/2020期望输出
location type increase percent_increase start_date end_date
ca cool 1 6.67% 10/05/2020 10/08/2020
ca name -5 -50% 10/6/2020 10/7/2020
ny hello 1 20% 10/01/2020 10/4/2020
ny ai 1 14.28% 10/2/2020 10/3/2020这就是我正在做的事情:
import pandas as pd
import numpy as np
df['date'] = pd.to_datetime(df['date'])
df1= df.groupby(pd.Grouper(key='Date', freq='7D')).sum()
df.groupby("type")["location"].()
df1['percent_increase'] = df1['Value'].pct_change().mul(100)
df1['increase'] = df1['Value'].diff() 我没有得到我想要的输出。任何帮助都是非常感谢的。
发布于 2020-11-03 11:37:03
我想你要找的是:
df.groupby('type')['size'].agg(lambda x:(x.iloc[-1]/x.iloc[0]-1)*100)当然,您可以将所需的其余列添加到此列中。
示例的输出:
ai 14.285714
cool 6.666667
hello 20.000000
name -50.000000https://stackoverflow.com/questions/64656216
复制相似问题