英文:
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
都像示例数据中一样总是按顺序排列为Monday
和Friday
,则解决方案是使用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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论