简体   繁体   English

确定熊猫数据框中每列的最大值

[英]determine column maximum value per another column in pandas dataframe

I have a dataframe contains location Id, store name and store revenue.我有一个包含位置 ID、商店名称和商店收入的数据框。 I want to determine the store that has the maximum revenue per area我想确定每个区域收入最高的商店

I wrote a code for that, but not sure if there is a better way to handle this case我为此写了一个代码,但不确定是否有更好的方法来处理这种情况

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)

and This is the required output:这是所需的输出: ![在此处输入图片说明

is there a better way to get this output有没有更好的方法来获得这个输出

Create boolean mask by compare by eq ( == ) and convert it to integer s - 0, 1 to False, True :通过eq ( == ) 比较创建布尔掩码并将其转换为integer s - 0, 1False, 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.

相关问题 对于Pandas数据框中的每一行,确定另一列中是否存在一列值 - For every row in Pandas dataframe determine if a column value exists in another column Pandas Dataframe 最大值列的名称 - Pandas Dataframe name of the column with maximum value 每个数据框列值的 Pandas COUNTIF - Pandas COUNTIF per dataframe column value Pandas:如何在由另一列分组的列上获取具有最大值 value_count 的行作为数据框 - Pandas: how to get the rows that has the maximum value_count on a column grouping by another column as a dataframe Pandas.Dataframe在第一列选择最大值,在另一列选择另一个条件。 - Pandas.Dataframe select maximum value on 1 column with another criteria on other column. 保持与 dataframe 中另一列的最大值对应的值 - Keep the value corresponding to the maximum of another column in a dataframe 从另一个数据帧中的列值替换pandas数据帧中的列中的值 - Replacing value in a column in pandas dataframe from a column value in another dataframe pandas - 根据另一列中的每个唯一值计算DataFrame中值的出现次数 - pandas - Counting occurrences of a value in a DataFrame per each unique value in another column Pandas:在 dataframe 中创建列,并通过查看另一个 dataframe 为该列分配值 - Pandas: Create column in dataframe and assign value to the column by looking into another dataframe Pandas:如何确定列值是否在另一列的列表中 - Pandas: How to determine if a column value is in another column's list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM