简体   繁体   中英

pandas adding new column to existing dataframe with condition

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

为此,您可以使用groupbyshift

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

您可以使用移位功能:

df['last_year_price'] = df.sort_values(by=['year'], ascending=True).groupby(['fruit'])['price'].shift(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM