Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x 如何基于dataframe python的前一行更新行_Python 3.x_Pandas_Time Series - Fatal编程技术网

Python 3.x 如何基于dataframe python的前一行更新行

Python 3.x 如何基于dataframe python的前一行更新行,python-3.x,pandas,time-series,Python 3.x,Pandas,Time Series,我的时间序列数据如下所示: date product price amount 11/01/2019 A 10 20 11/02/2019 A 10 20 11/03/2019 A 25 15 11/04/2019 C 40 50 11/05/2019 C 50 60 我有一个高维数据,我刚刚添加了两列{price,amount}的简化版本。我试图根据如下所示的时间索引对其进行相对转换: date product price amou

我的时间序列数据如下所示:

date    product price   amount
11/01/2019  A   10  20
11/02/2019  A   10  20
11/03/2019  A   25  15
11/04/2019  C   40  50
11/05/2019  C   50  60
我有一个高维数据,我刚刚添加了两列{price,amount}的简化版本。我试图根据如下所示的时间索引对其进行相对转换:

date    product price   amount
    11/01/2019  A   NaN NaN
    11/02/2019  A   0   0
    11/03/2019  A   15  -5
    11/04/2019  C   NaN NaN
    11/05/2019  C   10  10
我试图根据时间索引得到每个产品的相对变化。如果指定产品的上一个日期不存在,我将添加“NaN”


您能告诉我有什么功能可以执行此操作吗?

产品进行分组并使用
.diff()

输出:

        date product  price  amount
0 2019-11-01       A    NaN     NaN
1 2019-11-02       A    0.0     0.0
2 2019-11-03       A   15.0    -5.0
3 2019-11-04       C    NaN     NaN
4 2019-11-05       C   10.0    10.0

有没有办法插入不命名为[[“价格”、“金额”]]的列?我有一个高维数据,我正在尝试注入除{“date”,“product”}之外的列。当然,使用类似
cols=[“price”,“amount”]
的东西,如果你想要其他列,那么
others=[c代表df中的c。如果c不在(“price”,“amount”)]
中,那么你可以用
df[其他]选择所有
其他列
        date product  price  amount
0 2019-11-01       A    NaN     NaN
1 2019-11-02       A    0.0     0.0
2 2019-11-03       A   15.0    -5.0
3 2019-11-04       C    NaN     NaN
4 2019-11-05       C   10.0    10.0