![](/img/trans.png)
[英]For every row in Pandas dataframe determine if a column value exists in another column
[英]determine column maximum value per another column in pandas dataframe
我有一個包含位置 ID、商店名稱和商店收入的數據框。 我想確定每個區域收入最高的商店
我為此寫了一個代碼,但不確定是否有更好的方法來處理這種情況
import pandas as pd
dframe=pd.DataFrame({"Loc_Id":[1,2,2,1,2,1,3,3],"Store":["A","B","C","B","D","B","A","C"],
"Revenue":[50,70,45,35,80,70,90,65]})
#group by location id, then save max per location in new column
dframe["max_value"]=dframe.groupby("Loc_Id")["Revenue"].transform(max)
#create new column by checking if the revenue equal to max revenue
dframe["is_loc_max"]=dframe.apply(lambda x: 1 if x["Revenue"]==x["max_value"] else 0,axis=1)
#drop the intermediate column
dframe.drop(columns=["max_value"],inplace=True)
有沒有更好的方法來獲得這個輸出
通過eq
( ==
) 比較創建布爾掩碼並將其轉換為integer
s - 0, 1
到False, True
:
s = dframe.groupby("Loc_Id")["Revenue"].transform('max')
dframe["max_value"]= s.eq(dframe["Revenue"]).astype(int)
print (dframe)
Loc_Id Store Revenue max_value
0 1 A 50 0
1 2 B 70 0
2 2 C 45 0
3 1 B 35 0
4 2 D 80 1
5 1 B 70 1
6 3 A 90 1
7 3 C 65 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.