Reputation: 137
I'm trying to fill df['Group']
with either 'boys'
, 'girls'
, or 'both'
if their respective df['Color']
values exist within any of the lists or fill NaN
in df['Group']
if the df['Color']
value doesn't exist in any of the lists.
I have this:
boys = ['Brown', 'Green']
girls = ['Violet', 'Verde']
both ['Black', 'White']
Color | Group
---------------------
0 | 'Brown' | NaN
1 | 'Green' | NaN
2 | 'Black' | NaN
3 | 'White' | NaN
4 | 'Verde' | NaN
5 | 'Purple'| NaN
6 | 'Violet'| NaN
I want this:
Color | Group
---------------------
0 | 'Brown' | 'boys'
1 | 'Green' | 'boys'
2 | 'Black' | 'both'
3 | 'White' | 'both'
4 | 'Verde' | 'girls'
5 | 'Purple'| NaN
6 | 'Violet'| 'girls'
Upvotes: 0
Views: 572
Reputation: 17824
You can create a dictionary:
dct = dict(boys = ['Brown', 'Green'],
girls = ['Violet', 'Verde'],
both = ['Black', 'White'])
dct = {i: k for k, v in dct.items() for i in v}
Output:
{'Brown': 'boys',
'Green': 'boys',
'Violet': 'girls',
'Verde': 'girls',
'Black': 'both',
'White': 'both'}
Then you can use the method map
:
df['Group'] = df['Color'].map(dct)
Output:
Color Group
0 Brown boys
1 Green boys
2 Black both
3 White both
4 Verde girls
5 Purple NaN
6 Violet girls
Upvotes: 3