Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign values to column based on same values in another column

Tags:

python

pandas

I have a DataFrame that looks like this:

Market | Status | Team | Member |
-------|--------|------|--------|
Chicago|   1    |  ENG |  None  |
Chicago|   1    |  ENG |  None  |
SF Bay |   3    |  ENG |  Julia |

And a dictionary of users and their emails:

TeamMembers = {
    "Julia": "[email protected]", "Tyler": "[email protected]", "Kyle": "[email protected]"
}

In my DataFrame I want to randomly assign a Member if there is none, but if the Market value is the same, then the Member needs to also be the same.

I want to use

name, email = random.choice(list(TeamMembers.items()))

to get the specific names and email addresses but I'm not sure how to manipulate the DataFrame based on the Market being the same value.

like image 677
AGH_TORN Avatar asked Dec 06 '25 13:12

AGH_TORN


1 Answers

You can use transform with fillna, also generate only names by change items to keys:

df['Member'] = (df.groupby('Market')['Member']
                  .transform(lambda x: x.fillna(random.choice(list(TeamMembers.keys())))))
print (df)
    Market  Status Team Member
0  Chicago       1  ENG   Kyle
1  Chicago       1  ENG   Kyle
2   SF Bay       3  ENG  Julia
like image 128
jezrael Avatar answered Dec 12 '25 15:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!