在Pandas中,按另一列对数据进行分组,计算行之间的百分比变化。

huangapple go评论74阅读模式
英文:

Calculate Percent Change Between Rows in Pandas Grouped by Another Column

问题

Name Improvement
Al 17.33
Bo 11.11
Cy 11.76
英文:

I am somewhat new to Pandas and I have been stuck on a problem.

Assume I have the following dataframe (df1):

Name Day Score
Al Monday 75
Al Friday 88
Bo Monday 90
Bo Friday 100
Cy Monday 85
Cy Friday 95

I would like to create another dataframe (df2) with each person's name and their percent improvement from Monday to Friday.

The result would be:

Name Improvement
Al 17.33
Bo 11.11
Cy 11.76

For example, Al improved by 17.33% between Monday and Friday (((88-75)/75) * 100)

答案1

得分: 1

以下是翻译好的代码部分:

s = df.pivot('Name', 'Day', 'Score')
s = s.pct_change(-1, axis=1)['Friday'].reset_index(name='Improvement')

结果部分已包括在代码中,不需要额外的翻译。

英文:

Let us pivot to reshape then calculate pct change along column axis

s = df.pivot('Name', 'Day', 'Score')
s = s.pct_change(-1, axis=1)['Friday'].reset_index(name='Improvement')

Result

  Name  Improvement
0   Al     0.173333
1   Bo     0.111111
2   Cy     0.117647

答案2

得分: 1

如果每个Name都像示例数据中一样总是按顺序排列为MondayFriday,则解决方案是使用GroupBy.pct_change函数:

df = (df[['Name']].join(df.groupby('Name')['Score'].pct_change().mul(100)
                      .rename('Improvement'))
              .dropna())
print(df)

输出结果如下:

  Name  Improvement
1   Al    17.333333
3   Bo    11.111111
5   Cy    11.764706
英文:

If there is for each Name always ordered Monday and Friday like in sample data solution is GroupBy.pct_change:

df = (df[['Name']].join(df.groupby('Name')['Score'].pct_change().mul(100)
                          .rename('Improvement'))
                  .dropna())
print (df)
  Name  Improvement
1   Al    17.333333
3   Bo    11.111111
5   Cy    11.764706

huangapple
  • 本文由 发表于 2023年2月10日 13:21:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定