简体   繁体   English

如何检查多个列表中的任何一个中是否存在 DataFrame 列值,如果不存在,则填充另一列?

[英]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']内的任何名单的存在,或者填充值NaNdf['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.

相关问题 如何检查数据框中的另一列中是否存在列的唯一值? - How do i check that the unique values of a column exists in another column in dataframe? 如何根据另一列中的值检查 pandas df 列值是否存在? - How do I check if pandas df column value exists based on value in another column? 如何检查 DataFrame 列中是否存在元组值 - How to check if a tuple value exists in a DataFrame column 检查一个数据框中的值是否存在于另一个数据框中并创建列 - Check if value from one dataframe exists in another dataframe and create column 仅当某个列存在时,如何访问 dataframe 值? - How do I access a dataframe value only if a certain column exists? Pandas - 检查列中的值是否存在于 MultiIndex 数据帧的任何索引中 - Pandas - Check if value from a column exists in any index of a MultiIndex dataframe 如何在 dataframe 的 A 列中找到 B 列中的 dataframe 值,如果是,将 B 列中的值替换为 A 列的值? - How do I find in dataframe value in column B exists in Column A in a dataframe, and if so, replace the value in column B with Column A's value? 检查一列中的值是否存在于另一数据框中的多列中 - Check if values from one column, exists in multiple columns in another dataframe 如何在熊猫数据框中多次检查列中的值是否存在于同一列中 - how to check if a value in a column exists in same column multiple times in a pandas dataframe 如果另一列中存在任何值> 0,则需要为数据框分配值 - Need to assign values to a dataframe if any value >0 exists in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM