简体   繁体   English

计算pandas数据帧行之间的百分比差异

[英]Computing percentage difference between pandas dataframe rows

region  year      val
1.0     2015.0    6.775457e+05
1.0     2016.0    6.819761e+05
1.0     2017.0    6.864065e+05
2.0     2015.0    6.175457e+05
2.0     2016.0    6.419761e+05
3.0     2017.0    6.564065e+05

In the dataframe above, I want to compute the percentage difference between consecutive rows but only for the same region values. 在上面的数据框中,我想计算连续行之间的百分比差异,但仅针对相同的区域值。 I tried this but not sure if it works. 我试过这个,但不确定它是否有效。 What is best way to achieve it? 什么是实现它的最佳方法?

df.groupby(['region', 'year'])['val'].pct_change()

You can use DataFrameGroupBy.pct_change with groupby by column region : 您可以按列region使用DataFrameGroupBy.pct_change和groupby:

df['new'] = df.groupby('region')['val'].pct_change()
print (df)
   region    year       val       new
0     1.0  2015.0  677545.7       NaN
1     1.0  2016.0  681976.1  0.006539
2     1.0  2017.0  686406.5  0.006496
3     2.0  2015.0  617545.7       NaN
4     2.0  2016.0  641976.1  0.039560
5     3.0  2017.0  656406.5       NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM