[英]How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row?
I have the following code:我有以下代码:
import pandas as pd
import numpy as np
df = pd.DataFrame([['red', 1], ['red', 13], ['red', 1], ['blue', 1], ['red', 112], ['blue', 10]])
df.columns = ["colour","rank"]
# df['highest_rank'] = ...
print(df)
"""
colour rank highest_rank
0 red 1 122
1 red 13 122
2 red 1 122
3 blue 1 10
4 red 112 122
5 blue 10 10
"""
Hopefully, the example can show you what I'm trying to do as I'm struggling to describe what I'm wanting - The highest ranking of each colour.希望该示例可以向您展示我正在努力做的事情,因为我正在努力描述我想要的东西 - 每种颜色的最高排名。
groupby colour and broadcast the highest rank in each group using transform. groupby 颜色并使用变换广播每组中的最高排名。 Code below下面的代码
df['highest_rank']=df.groupby('colour')['rank'].transform('max')
colour rank highest_rank
0 red 1 112
1 red 13 112
2 red 1 112
3 blue 1 10
4 red 112 112
5 blue 10 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.