2

I have a pandas data frame like so.

fruit year price
apple 2018 4
apple 2019 3
apple 2020 5
plum 2019 3
plum 2020 2

and I want to add column [last_year_price]

please help......

dmd404
  • 23
  • 3

3 Answers3

1

For this, you can use groupby and shift:

df['last_year_price'] = df.groupby('fruit').shift(1).price
David
  • 871
  • 1
  • 5
  • 13
0

You can use the shift function:

df['last_year_price'] = df.sort_values(by=['year'], ascending=True).groupby(['fruit'])['price'].shift(1)
Gal Moran
  • 97
  • 3
-1

Use DataFrameGroupBy.idxmax for rows with maximal years and join to oriinal DataFrame:

df = df.merge(df.loc[df.groupby('fruit')['year'].idxmax(), ['fruit','price']].rename(columns={'price':'last_year_price'}), on='fruit', how='left')
print (df)
   fruit  year  price  last_year_price
0  apple  2018      4                5
1  apple  2019      3                5
2  apple  2020      5                5
3   plum  2019      3                2
4   plum  2020      2                2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252