通过计算百分比变化列

2021-11-29 22:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚接触熊猫数据帧,需要帮助理解百分比变化。你知道吗

我确实从一个查询生成了一个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

在那之后,我计算了第二天“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'  

平均值的最终输出:

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

有人能帮我计算一下1周和3周的交通量变化百分比吗?谢谢!!你知道吗