Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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 - Fatal编程技术网

Python 基于具有条件的其他列的值在熊猫中添加列

Python 基于具有条件的其他列的值在熊猫中添加列,python,pandas,Python,Pandas,我有一个数据框,其中包含一些产品(单位)的销售信息: 我想为每次销售添加列,列中包含以前销售的信息,如果没有以前的销售,则列中包含NaN unit year month price prev_price prev_year prev_month 0 1 2018 6 100 70.0 2013.0 4.0 1 1 2013 4 70 NaN

我有一个
数据框
,其中包含一些产品(单位)的销售信息:

我想为每次销售添加列,列中包含以前销售的信息,如果没有以前的销售,则列中包含NaN

    unit    year    month   price   prev_price  prev_year   prev_month
0   1       2018    6       100      70.0        2013.0      4.0
1   1       2013    4        70      NaN         NaN         NaN
2   2       2015    10       80      110.0       2015.0      2.0
3   2       2015    2       110      NaN         NaN         NaN
4   3       2017    4       120      90.0        2002.0      6.0
5   3       2002    6        90      NaN         NaN         NaN
6   4       2016    1        55      NaN         NaN         NaN
目前,我正在对单元进行一些
分组
,保留那些有多行的单元,然后提取与最小日期关联的这些单元的信息。然后将此表与原始表合并,只保留合并后的两个表中日期不同的行。 我觉得有一种非常简单的方法可以做到这一点,但我不确定如何做到。

与新的
数据帧一起使用,并将其附加到原始数据帧中:

#if real data are not sorted
#df = df.sort_values(['unit','year','month'], ascending=[True, False, False])

df = df.join(df.groupby('unit', sort=False).shift(-1).add_prefix('prev_'))
print (df)
   unit  year  month  price  prev_year  prev_month  prev_price
0     1  2018      6    100     2013.0         4.0        70.0
1     1  2013      4     70        NaN         NaN         NaN
2     2  2015     10     80     2015.0         2.0       110.0
3     2  2015      2    110        NaN         NaN         NaN
4     3  2017      4    120     2002.0         6.0        90.0
5     3  2002      6     90        NaN         NaN         NaN
6     4  2016      1     55        NaN         NaN         NaN

谢谢,但如果2015/02在第2行,2015/10在第3行,这不起作用,因为groupby将保留2015/10的价值,这不是最新的销售,在我的原始数据中,销售是按任何顺序进行的。我知道我以前可以简单地对数据集进行排序,但我想知道是否存在不改变数据集顺序的解决方案。@Wendy-我认为您需要有序数据,否则可能存在解决方案,但复杂且缓慢。
#if real data are not sorted
#df = df.sort_values(['unit','year','month'], ascending=[True, False, False])

df = df.join(df.groupby('unit', sort=False).shift(-1).add_prefix('prev_'))
print (df)
   unit  year  month  price  prev_year  prev_month  prev_price
0     1  2018      6    100     2013.0         4.0        70.0
1     1  2013      4     70        NaN         NaN         NaN
2     2  2015     10     80     2015.0         2.0       110.0
3     2  2015      2    110        NaN         NaN         NaN
4     3  2017      4    120     2002.0         6.0        90.0
5     3  2002      6     90        NaN         NaN         NaN
6     4  2016      1     55        NaN         NaN         NaN