简体   繁体   English

Python Pandas:根据其他列中的唯一标识符创建具有最小值的新列

[英]Python Pandas: create new column with min values based on unique identifiers in other columns

I have a dataframe:我有一个 dataframe:

pd.DataFrame({'person':['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'], 
              'bank':['chase', 'bod', 'chase', 'boa', 'chase', 'bod', 'chase', 'boa'],
              'amount': [100, 80, 90, 60, 150, 111, 524, 51]})

在此处输入图像描述

Assuming there could be many people in the "person" column.假设“人”列中可能有很多人。 But there are only "chase" and "boa" in the "bank" column.但“银行”一栏中只有“追逐”和“蟒蛇”。 Every person will have both "chase" and "boa".每个人都会有“追逐”和“蟒蛇”。 For each person, I want to get the minimum number in the "amount" column for each bank.对于每个人,我想在每家银行的“金额”列中获得最小数量。 The output will be like this: output 将是这样的:

在此处输入图像描述

Each row should have the person, min amount in chase ("chase_min") and min amount in boa ("boa_min").每行应该有人,追逐中的最小数量(“chase_min”)和蟒蛇中的最小数量(“boa_min”)。

Thank you!谢谢!

You can use min() as the aggfunc for a pivot table.您可以使用 min() 作为 pivot 表的 aggfunc。

pd.pivot_table(df, index='person', columns=['bank'], values='amount', aggfunc='min')

I think the current answer is overkill, and this solution has the advantage of producing a convenient index:我认为当前的答案是矫枉过正,这个解决方案的优点是产生一个方便的索引:

import pandas as pd

df = pd.DataFrame({'person': ['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y'],
                   'bank': ['chase', 'bod', 'chase', 'boa', 'chase', 'bod', 'chase', 'boa'],
                   'amount': [100, 80, 90, 60, 150, 111, 524, 51]})

res = df.groupby(["person", "bank"]).min()

print(f"{df}\n\n{res}")

Output: Output:

  person   bank  amount
0      x  chase     100
1      x    bod      80
2      x  chase      90
3      x    boa      60
4      y  chase     150
5      y    bod     111
6      y  chase     524
7      y    boa      51

              amount
person bank         
x      boa        60
       bod        80
       chase      90
y      boa        51
       bod       111
       chase     150

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

相关问题 根据熊猫中其他列的值添加具有唯一标识符的列 - Add column with unique identifiers based on values from other columns in pandas pandas,根据其他两列的值创建一个新的唯一标识符列 - pandas, create a new unique identifier column based on values from two other columns Python pandas 根据一列的唯一值创建多列 - Python pandas create multiple columns based on unique values of one column 根据其他 pandas 列中列表中的值数创建新列? - Create new columns based on number of values in list in other pandas column? 根据其他列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in other columns 根据其他列(python)中的分类值创建新的pandas列 - Create new pandas column based on categorical values in other column (python) 基于python pandas中其他列的值创建新列 - Creating a new column based on values from other columns in python pandas Python Pandas 基于其他列值的新列 - Python Pandas New Column based on values from other columns 如何根据pandas中其他列的值计算新列 - python - how to compute a new column based on the values of other columns in pandas - python Python groupby-根据其他列中的值创建一个新列 - Python groupby - Create a new column based on values in other columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM