Home > OS >  pandas adding new column to existing dataframe with condition
pandas adding new column to existing dataframe with condition

Time:07-07

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......

CodePudding user response:

For this, you can use groupby and shift:

df['last_year_price'] = df.groupby('fruit').shift(1).price

CodePudding user response:

Use:

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

CodePudding user response:

Firstly, create a list containing all the values that you want to have in your "last_year_price" column.

Secondly, simply type: df['last_year_price] = list_of_values and that should do the trick (replace df for whatever you named your dataframe).

  • Related