Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 表计算运行指数_Python_Pandas_Pivot Table - Fatal编程技术网

Python 表计算运行指数

Python 表计算运行指数,python,pandas,pivot-table,Python,Pandas,Pivot Table,我有一个数据帧 import pandas as pd df = pd.DataFrame({ 'PART_UNIT': ['A','A','A','A','A'], 'FiscalYear': ['2015/2016','2016/2017','2017/2018','2018/2019','2019/2020'], 'BUY_UNIT_PRICE': [30,32,33,31,35] }) 我已将其转换为pivot_表 pivot = df.pivot_table(

我有一个数据帧

import pandas as pd
df = pd.DataFrame({
    'PART_UNIT': ['A','A','A','A','A'],
    'FiscalYear': ['2015/2016','2016/2017','2017/2018','2018/2019','2019/2020'],
    'BUY_UNIT_PRICE': [30,32,33,31,35]
})
我已将其转换为pivot_表

pivot = df.pivot_table(index='PART_UNIT', columns='FiscalYear', values='BUY_UNIT_PRICE', aggfunc='mean')
print(pivot)

FiscalYear  2015/2016  2016/2017  2017/2018  2018/2019  2019/2020
PART_UNIT
A                  30         32         33         31         35
我正在寻求帮助,以确定每年的发展

  • 2015/2016=指数100
  • 2016/2017=(2016/2017年价值)/(2015/2016年价值)*100
  • 2017/2018=(2017/2018年价值)/(2016/2017年价值)*100
我希望这个示例数据的输出是

FiscalYear  2015/2016  2016/2017  2017/2018  2018/2019  2019/2020
PART_UNIT
A                 100     106.67     103,13      93,94      112,9
我将如何在熊猫身上做这件事

我所追求的函数与excel pivot表中的函数相同,在excel pivot表中,您可以选择“将值显示为上一年的百分比”

您可以对轴=1进行一次遍历,然后再乘以100,最后再乘以100,以匹配预期的输出:

m=(df.pivot_table(index='PART_UNIT', columns='FiscalYear', 
                       values='BUY_UNIT_PRICE', aggfunc='mean'))



我只在2016/2017年和2017/2018年有一个价格,我希望2016/2017年的指数是100,2017/2018年的指数是103.125,其他字段应该是100NULL@HenrikPoulsen那么
m.where(m.isna(),m.pct_change(axis=1).mul(100).add(100,fill_value=0))呢
m.pct_change(axis=1).mul(100).add(100,fill_value=0)
FiscalYear  2015/2016   2016/2017  2017/2018  2018/2019   2019/2020
PART_UNIT                                                          
A               100.0  106.666667    103.125  93.939394  112.903226