[英]Pandas groupby calculating percentage change column
I'm new to pandas dataframes and I need help in understanding percentage changes.我是熊猫数据框的新手,我需要帮助来理解百分比变化。
I did generate a csv from a query in order to calculate mean values by assigning ranking to the columns.我确实从查询中生成了一个 csv,以便通过为列分配排名来计算平均值。
rank ds continent region device traffic
1 08/13 North america US mobile 7300
1 08/13 North america US desktop 2500
2 08/06 Europe UK mobile 3300
2 08/06 Europe Italy desktop 5600
And after that I did calculate mean traffic for '1 week' and '3 week' in the second csv.之后,我确实在第二个 csv 中计算了“1 周”和“3 周”的平均流量。
df_1 = df.loc[df['rank'] == '1']
df_1['traffic'] = df_1['traffic'].astype(float).fillna(0)
avg_1 = df_1.groupby(['continent','region','device']).mean()
avg_1['ds'] = '1 week'
last_3 = df.loc[df['rank'].isin(['2','3','4'])]
last_3['traffic'] = last_3['traffic'].astype(float).fillna(0)
avg_3 = last_3.groupby(['continent','region','device']).mean()
avg_3['ds'] = '3 week'
Final output for mean:均值的最终输出:
market country traffic device ds
North america US 36015.33 mobile 1week
North america US 40663.67 desktop 3week
Europe UK 360270.7 mobile 1week
Europe Italy 1363183 desktop 3week
Can anyone help me to calculate the percentage change traffic as a separate column for 1week and 3 week?任何人都可以帮助我计算 1 周和 3 周的百分比变化流量作为单独的列吗? Thanks!!谢谢!!
Got this figured out.这个想通了Used pct_change()使用 pct_change()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.