[英]Keep the value corresponding to the maximum of another column in a dataframe
I have a DataFrame我有一个 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.