简体   繁体   English

如何使用熊猫创建一个新列的最大值(对应于特定名称)?

[英]How do I create a new column of max values of a column(corresponding to specific name) using pandas?

I'm wondering if it is possible to use Pandas to create a new column for the max values of a column (corresponding to different names, so that each name will have a max value).我想知道是否可以使用 Pandas 为列的最大值创建一个新列(对应于不同的名称,以便每个名称都有一个最大值)。

For an example:例如:

name    value    max
Alice    1        9
Linda    1        1
Ben      3        5
Alice    4        9
Alice    9        9
Ben      5        5
Linda    1        1

So for Alice, we are picking the max of 1, 4, and 9, which is 9. For Linda max(1,1) = 1, and for Ben max(3,5) = 5.所以对于 Alice,我们选择 1、4 和 9 中的最大值,即 9。对于 Linda max(1,1) = 1,对于 Ben max(3,5) = 5。

I was thinking of using .loc to select the name == "Alice" , then get the max value of these rows, then create the new column.我正在考虑使用.loc选择name == "Alice" ,然后获取这些行的最大值,然后创建新列。 But since I'm dealing with a large dataset, this does not seem like a good option.但由于我正在处理一个大型数据集,这似乎不是一个好的选择。 Is there a smarter way to do this so that I don't need to know what specific names?有没有更聪明的方法来做到这一点,这样我就不需要知道具体的名字了?

groupby and taking a max gives the max by name, which is then merged with the original df groupby 并取一个 max 按名称给出最大值,然后将其与原始 df 合并

df.merge(df.groupby(['name'])['value'].max().reset_index(), 
         on='name').rename(
                    columns={'value_x' : 'value', 
                             'value_y' : 'max'})
    name    value   max
0   Alice   1   9
1   Alice   4   9
2   Alice   9   9
3   Linda   1   1
4   Linda   1   1
5   Ben     3   5
6   Ben     5   5

You could use transform or map您可以使用transformmap

df['max'] = df.groupby('name')['value'].transform('max')

or或者

df['max'] = df['name'].map(df.groupby('name')['value'].max())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用计算值在 pandas 中创建新列并为每一行分配特定值? - How do I create a new column in pandas using calculated values and assign specific values to each row? 如何创建一个新列,该列是在Pandas Dataframe中相似值分组在一起的另一列的最大值? - How do I create a new column which is the max of another column where similar values grouped together in Pandas Dataframe? 如何创建具有特定 dtype 的新 Pandas 列? - How do I create a new Pandas column with a specific dtype? 在pandas数据框中创建新列,以合并特定的列名称和相应的值 - Create new column in pandas dataframe that merges specific column names and corresponding values 如何使用 pandas 根据第三列中的值创建一个包含来自一列或另一列的值的新列? - How do I create a new column with values from one column or another based on the value in a third column using pandas? 如何使用Pandas在CSV文件中创建新列,并根据这些列中的值添加数据 - How do I create a new column in a csv file using Pandas, and add data depending on the values in those columns 如何获取熊猫数据框列的最大值并在另一列中找到相应的值? - How do I take max value of a pandas dataframe column and find the corresponding value in another column? 根据条件,用相应的列名替换 pandas 数据框中的特定值, - Replace specific values in pandas dataframe with the corresponding column name, based on a condition, Pandas:如何重命名多级索引列或创建新列? - Pandas: How do I re-name a multi-level indexed column or create a new column? 如何比较一列中的值并使用pandas创建一个新列? - How to compare values in a column and create a new column using pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM