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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With