[英]Groupby replace values with there max value pandas
I have this DataFrame我有这个 DataFrame
lst = [['AAA',15],['BBB',16],['BBB',22],['AAA',20],['CCC',11],['AAA',10]]
df = pd.DataFrame(lst,columns = ['name','val'])
which looks like this看起来像这样
name val
0 AAA 15
1 BBB 16
2 BBB 22
3 AAA 20
4 CCC 11
5 AAA 10
I want this我要这个
name val
0 AAA 20
1 BBB 22
2 BBB 22
3 AAA 20
4 CCC 11
5 AAA 20
replaced all val
with the maximum of there name
group用name
组的最大值替换所有val
I did this so far到目前为止我做到了
dd = df.groupby('name')['val'].max().to_dict()
which will give me the dictionary of all the max val
now i have to replace them using this dictionary.这将为我提供所有最大val
的字典,现在我必须使用这本字典替换它们。
If i do this after this will replace all the name
with val
but i want to replace all the val
according to there name
如果我这样做之后会用val
替换所有name
,但我想根据那里的name
替换所有val
df.replace({"name": dd})
lst = [['AAA',15],['BBB',16],['BBB',22],['AAA',20],['CCC',11],['AAA',10]]
df = pd.DataFrame(lst,columns = ['name','val'])
max = df.groupby('name').max()
df=df.merge(max,on='name')
del df['val_x']
print(df)
name val_y
0 AAA 20
1 AAA 20
2 AAA 20
3 BBB 22
4 BBB 22
5 CCC 11
If you want/need to go through a dictionary, you could DataFrame.apply() a function to facilitate the changes:如果您想/需要通过字典 go ,您可以DataFrame.apply()一个 function 以方便更改:
import pandas as pd
lst = [['AAA',15],['BBB',16],['BBB',22],['AAA',20],['CCC',11],['AAA',10]] df = pd.DataFrame(lst,columns = ['name','val']) dd = df.groupby('name')['val'].max().to_dict()
df["val"] = df["name"].apply(lambda d: dd[d])
print (df)
or as pointed out by Ch3steR in the comment或正如评论中Ch3steR所指出的那样
df["val"] = df["name"].map(dd)
looks even smarter.看起来更聪明。 See Series.map() .请参阅Series.map() 。
Output: Output:
name val
0 AAA 20
1 BBB 22
2 BBB 22
3 AAA 20
4 CCC 11
5 AAA 20
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.