Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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 如何在Pandas DataFrame中区分面板数据_Python_Pandas - Fatal编程技术网

Python 如何在Pandas DataFrame中区分面板数据

Python 如何在Pandas DataFrame中区分面板数据,python,pandas,Python,Pandas,我想知道是否有任何简单的Python命令或包可以让我轻松地将变量添加到data.frames中,这是这些变量随时间的“差异”或变化 如果我的数据如下所示: Day Price Good --- ------- -- 1 1 8 apples 2 2 10 apples 3 3 7 apples 4 4 11 apples 5 5 14 apples 6 1 12

我想知道是否有任何简单的Python命令或包可以让我轻松地将变量添加到data.frames中,这是这些变量随时间的“差异”或变化

如果我的数据如下所示:

   Day Price    Good
  ---  -------  -- 
1  1     8      apples
2  2    10      apples
3  3     7      apples
4  4    11      apples
5  5    14      apples
6  1    12      oranges
7  2    11      oranges
8  3     9      oranges
9  4    14      oranges
10 5    11      oranges

在对价格变量进行“第一次差分”之后,我的数据如下所示

   Day Price    Good P1d
1    1     8  apples  NA
2    2    10  apples   2
3    3     7  apples  -3
4    4    11  apples   4
5    5    14  apples   3
6    1    12 oranges  NA
7    2    11 oranges  -1
8    3     9 oranges  -2
9    4    14 oranges   5
10   5    11 oranges  -3

使用
.groupby()
后跟
.diff()

印刷品:

日价格良好P1d
1 18苹果南
210个苹果2.0
37个苹果-3.0
苹果4.0
514苹果3.0
6112个橙子
7.2.11橙子-1.0
8 3 9个橙子-2.0
9 4 14橙子5.0
10 5 11橙子-3.0
使用
.groupby()
后跟
.diff()

印刷品:

日价格良好P1d
1 18苹果南
210个苹果2.0
37个苹果-3.0
苹果4.0
514苹果3.0
6112个橙子
7.2.11橙子-1.0
8 3 9个橙子-2.0
9 4 14橙子5.0
10 5 11橙子-3.0
df["P1d"] = df.groupby("Good")["Price"].diff()
print(df)