熊猫:1个数据框比较行以创建新列

时间:2018-09-19 09:58:47

标签: pandas

您是stackoverflow的好人,

我有一个问题,似乎无法解决。

df1如下:

Group     item     Quarter    price    quantity

1         A        2017Q3     0.10     1000 
1         A        2017Q4     0.11     1000 
1         A        2018Q1     0.11     1000
1         A        2018Q2     0.12     1000 
1         A        2018Q3     0.11     1000

所需结果是一个新数据框,其名称为df2,并带有附加列。

Group     item     Quarter    price    quantity    savings/lost

1         A        2017Q3     0.10     1000         0.00   
1         A        2017Q4     0.11     1000         0.00
1         A        2018Q1     0.11     1000         0.00
1         A        2018Q2     0.12     1000         0.00
1         A        2018Q3     0.11     1000         10.00
1         A        2018Q4     0.13     1000         -20.00

本质上,我想逐行浏览,查看季度并查找去年 进行类似的季度计算(本季度的价格-上一季度的价格*数量)。如果没有上一季度的数据,则在最后一列。

为了完整说明,这里有更多的组和项目,甚至还有更多季度,例如2016Q1、2017Q1、2018Q1,尽管我只需要比较前一年。宿舍为字符串格式。

任何帮助将不胜感激。 谢谢!

1 个答案:

答案 0 :(得分:0)

使用pandas.DataFrame.shift

下面的代码假定您的列Quarter已排序并且没有丢失的四分之一。 您可以尝试以下代码:

# Input dataframe
  Group item Quarter  price  quantity
0     1    A  2017Q3   0.10      1000
1     1    A  2017Q4   0.11      1000
2     1    A  2018Q1   0.11      1000
3     1    A  2018Q2   0.12      1000
4     1    A  2018Q3   0.11      1000
5     1    A  2018Q4   0.13      1000

# Code to generate your new column 'savings/lost'
df['savings/lost'] =  df['price'] * df['quantity'] - df['price'].shift(4) * df['quantity'].shift(4)

# Output dataframe
  Group item Quarter  price  quantity  savings/lost
0     1    A  2017Q3   0.10      1000           NaN
1     1    A  2017Q4   0.11      1000           NaN
2     1    A  2018Q1   0.11      1000           NaN
3     1    A  2018Q2   0.12      1000           NaN
4     1    A  2018Q3   0.11      1000          10.0
5     1    A  2018Q4   0.13      1000          20.0

希望这对您有所帮助。

更新

我已更新代码以处理两件事,首先对Quarter进行排序,然后对遗失的Quarter场景进行处理。对于基于列的分组,您可以参考pandas.DataFrame.groupby和本网站中已经回答的许多pd.groupby相关问题。

#Input dataframe
  Group item Quarter  price  quantity
0     1    A  2014Q3   0.10       100
1     1    A  2017Q2   0.16       800
2     1    A  2017Q3   0.17       700
3     1    A  2015Q4   0.13       400
4     1    A  2016Q1   0.14       500
5     1    A  2014Q4   0.11       200
6     1    A  2015Q2   0.12       300
7     1    A  2016Q4   0.15       600
8     1    A  2018Q1   0.18       600
9     1    A  2018Q2   0.19       500

#Code to do the operations
df.index = pd.PeriodIndex(df.Quarter, freq='Q')
df.sort_index(inplace=True)
df2 = df.reset_index(drop=True)
df2['Profit'] = (df.price * df.quantity) - (df.reindex(df.index - 4).price * df.reindex(df.index - 4).quantity).values
df2['Profit'] = np.where(np.in1d(df.index - 4, df.index.values),
                        df2.Profit, ((df.price * df.quantity) - (df.price.shift(1) * df.quantity.shift(1))))
df2.Profit.fillna(0, inplace=True)

#Output dataframe
  Group item Quarter  price  quantity  Profit
0     1    A  2014Q3   0.10       100     0.0
1     1    A  2014Q4   0.11       200    12.0
2     1    A  2015Q2   0.12       300    14.0
3     1    A  2015Q4   0.13       400     0.0
4     1    A  2016Q1   0.14       500    18.0
5     1    A  2016Q4   0.15       600     0.0
6     1    A  2017Q2   0.16       800    38.0
7     1    A  2017Q3   0.17       700    -9.0
8     1    A  2018Q1   0.18       600   -11.0
9     1    A  2018Q2   0.19       500     0.0

我希望这是解决此处所述问题的方法。

相关问题