I have a DataFrame
day type price
0 10900 2 300
1 10900 1 500
2 10900 3 200
3 10901 5 100
4 10901 2 400
5 10901 1 300
6 10902 2 100
7 10902 3 300
8 10902 1 200
9 10902 4 400
and i want for each day to keep the price of the highest type and replicated on all the prices of that day so the result will be:
day type price
0 10900 2 200
1 10900 1 200
2 10900 3 200
3 10901 5 100
4 10901 2 100
5 10901 1 100
6 10902 2 400
7 10902 3 400
8 10902 1 400
9 10902 4 400
so this is what i am doing and i am wondering if it is the best way or if there is a way to not use the merge and make the group by in another way:
tmp_df=df[df.groupby('day')['type'].transform('max') == df['type']].reset_index()
df=df[['day','type']].merge(tmp_df[['day','price']],on='day')
df["price"] = df.groupby("day", as_index=False)["price"].transform(
lambda x: df.loc[df.loc[x.index, "type"].idxmax(), "price"]
)
print(df)
Prints:
day type price
0 10900 2 200
1 10900 1 200
2 10900 3 200
3 10901 5 100
4 10901 2 100
5 10901 1 100
6 10902 2 400
7 10902 3 400
8 10902 1 400
9 10902 4 400
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.