python - 计算 Pandas 数据框行之间的百分比差异

标签 python pandas

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

在上面的数据框中,我想计算连续行之间的百分比差异,但仅限于相同的区域值。我试过了但不确定它是否有效。实现它的最佳方法是什么?

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

最佳答案

您可以使用 DataFrameGroupBy.pct_change按列 region 分组:

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

关于python - 计算 Pandas 数据框行之间的百分比差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45687145/

相关文章:

python - 在 Windows 8 上安装并运行 Django

python - 如何计算长时间序列数据中连续事件的数量

Python 将日期转换和聚合到月份列

python - 应用函数来操作 Python Pandas DataFrame 组

python - 根据使用正则表达式获得的另一列上的匹配替换列上的值 (Python Pandas)

python - 如果调用程序结束,Popen 对象会发生什么

python - 同步时未记录的 Exchange ActiveSync 状态

python - selenium.common.exceptions.WebDriverException : Message: unknown error: DevToolsActivePort file doesn't exist with chromium browser and Selenium Python

Python多处理池: how to join the reasults in a parallel way?

python - 从具有日期范围的 DataFrame 创建 Pandas 每日聚合时间序列