[英]How do I check if a DataFrame column value exists in any of multiple lists, and if not, fill another column?
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.我试图填补df['Group']
与任一'boys'
, 'girls'
,或者'both'
如果他们各自的df['Color']
内的任何名单的存在,或者填充值NaN
在df['Group']
如果df['Color']
值不存在于任何列表中。
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'
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
:然后你可以使用方法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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.