【问题标题】:pandas: replace corresponding values in a comma separated column based on a list and another column conditionspandas:根据列表和另一列条件替换逗号分隔列中的相应值
【发布时间】:2021-12-13 17:13:01
【问题描述】:

我有一个数据框和一个列表如下:

import pandas as pd
import numpy as np

df = pd.DataFrame({'IDs':['d,f,o','d,f','d,f,o','d,f','d,f'],
                'Names':['APPLE ABCD ONE','date ABCD','NO foo YES','ORANGE AVAILABLE','TEA AVAILABLE']})

my_list = ['APPLE', 'ORANGE', 'LEMONS', 'STRAWBERRY', 'BLUEBERRY']

我想将 IDs 列中的逗号分隔值替换为 Names 列中的相应值,以防它们出现在 my_list 中。

desired output:
df.IDs => ['APPLE,f,o', 'd,f', 'd,f,o', 'ORANGE,f', 'd,f']

找出该行是否包含我尝试过的列表中的值:

df['Names'].apply(lambda x: any([k in x for k in my_list]))

为了替换 IDs 列中的值,我尝试了以下方法,但我不确定如何指示只有相应的值应该更改,

df.IDs.apply(lambda i: i if i in my_list else 'don't know what to do here')

我想我可以使用 np.where() 根据条件执行整个替换

np.where(df['Names'].apply(lambda x: any([k in x for k in my_list])) == True, df.IDs.apply(lambda i: i if i in my_list else 'don't know what to do here'), df.IDs)

【问题讨论】:

    标签: python pandas list apply str-replace


    【解决方案1】:

    您可以split/explode,然后从列表中替换您的值,然后将agg 恢复为原始形状:

    (df.assign(IDs=df['IDs'].str.split(','),     # strings to lists
               Names=df['Names'].str.split(' ')
              )
       .apply(pd.Series.explode)                 # lists to rows
        # map the Names in place of Ids is in my_list
       .assign(IDs=lambda d: d['IDs'].mask(d['Names'].isin(my_list), d['Names']))
        # reshape back to original by joining
       .groupby(level=0).agg({'IDs': ','.join, 'Names': ' '.join})
    )
    

    输出:

             IDs             Names
    0  APPLE,f,o    APPLE ABCD ONE
    1        d,f         date ABCD
    2      d,f,o        NO foo YES
    3   ORANGE,f  ORANGE AVAILABLE
    4        d,f     TEA AVAILABLE
    

    【讨论】:

    • 非常感谢您的解释
    猜你喜欢
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2012-04-09
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多