0

I have a pandas df that looks like the following

df = pd.DataFrame({'Amount': [1,2,3,4,6,7],
                'Name': ['person1', 'person1' ,'person2'     ,'person2','person3','person3'],})

What I am trying to do is create a third column that displays the minimum amount for each person. What is the most efficient way to do this?

0

1 Answer 1

2

The key is using groupby, which is so useful that I strongly recommend reading the section of the docs linked there. You can get a Series with the minimum values per person:

>>> df.groupby("Name")["Amount"].min()
Name
person1    1
person2    3
person3    6
Name: Amount, dtype: int64

Or the same information as a DataFrame:

>>> df.groupby("Name", as_index=False)["Amount"].min()
      Name  Amount
0  person1       1
1  person2       3
2  person3       6

[3 rows x 2 columns]

Or if you really want to add a column to your original dataframe, you can use transform:

>>> df["Minimum_Amount"] = df.groupby("Name")["Amount"].transform('min')
>>> df
   Amount     Name  Minimum_Amount
0       1  person1               1
1       2  person1               1
2       3  person2               3
3       4  person2               3
4       6  person3               6
5       7  person3               6

[6 rows x 3 columns]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.