【问题标题】:How to replace a value that is included in a list in multiple pandas columns如何替换多个熊猫列中列表中包含的值
【发布时间】:2020-06-13 13:52:13
【问题描述】:

我有以下df:

                      Q4_1                Q4_2            Q4_3        Q4_4  \
130              far cry 4                 NaN             NaN         NaN   
131       grand theft auto     Assassins Creed    call of duty  watch dogs   
132    the elder scrolls v       the witcher 3      dragon age  dark souls   
133              Uncharted        Call of duty        Deadpool     Far cry   
134  Monster Hunter: World  Kingdom Hearts III  Darksiders III         NaN   
135              Fallout 4                 NaN             NaN         NaN   

                 Q4_5         Q4_6       Q4_7 Q4_8 Q4_9 Q4_10  
130               NaN          NaN        NaN  NaN  NaN   NaN  
131               NaN          NaN        NaN  NaN  NaN   NaN  
132           fallout  assassins creed  fallout 3  NaN  NaN   NaN  
133  Grand theft auto          NaN        NaN  NaN  NaN   NaN  
134               NaN          NaN        NaN  NaN  NaN   NaN  
135               NaN          NaN        NaN  NaN  NaN   NaN  

我想用基于列表的唯一标识字符串替换相似的字符串。所以这将是所需的输出:

                      Q4_1                Q4_2            Q4_3        Q4_4  \
130              far cry 4                 NaN             NaN         NaN   
131       grand theft auto     Assassin's Creed    call of duty  watch dogs   
132    the elder scrolls v       the witcher 3      dragon age  dark souls   
133              Uncharted        Call of duty        Deadpool     Far cry   
134  Monster Hunter: World  Kingdom Hearts III  Darksiders III         NaN   
135              Fallout 4                 NaN             NaN         NaN   

                 Q4_5         Q4_6       Q4_7 Q4_8 Q4_9 Q4_10  
130               NaN          NaN        NaN  NaN  NaN   NaN  
131               NaN          NaN        NaN  NaN  NaN   NaN  
132           fallout  Assassin's Creed  fallout 3  NaN  NaN   NaN  
133  Grand theft auto          NaN        NaN  NaN  NaN   NaN  
134               NaN          NaN        NaN  NaN  NaN   NaN  
135               NaN          NaN        NaN  NaN  NaN   NaN  

所以我已经得到了一组可以找到的值:

list_assasins_creed = ['Assasin\'s Creed', 'Assassin\'s Creed', 'Assassins Creed', 'assasins creed', 'assassin\'s creed', 'assassins creed']

这些值可以在多个列中找到 (Q4_1..Q4_9)

我想用刺客信条替换这些值。 我尝试使用 loc:

df_survey_Q4.loc[df_survey_Q4[['Q4_1', 'Q4_2', 'Q4_3', 'Q4_4', 'Q4_5', 'Q4_6', 'Q4_7', 'Q4_8','Q4_9', 'Q4_10']].isin(list_assasins_creed),['Q4_1', 'Q4_2', 'Q4_3', 'Q4_4', 'Q4_5', 'Q4_6', 'Q4_7', 'Q4_8','Q4_9', 'Q4_10']] = 'Assassin\'s Creed'

但我得到了这个错误:

KeyError: "None of [Index([     ('Q', '4', '_', '1'),      ('Q', '4', '_', '2'),\n            ('Q', '4', '_', '3'),      ('Q', '4', '_', '4'),\n            ('Q', '4', '_', '5'),      ('Q', '4', '_', '6'),\n            ('Q', '4', '_', '7'),      ('Q', '4', '_', '8'),\n            ('Q', '4', '_', '9'), ('Q', '4', '_', '1', '0')],\n      dtype='object')] are in the [index]"

知道我该怎么做吗?

【问题讨论】:

标签: python pandas string replace


【解决方案1】:

你可以这样写代码,

df.replace( ['Assasin\'s Creed', 'Assassin\'s Creed', 'Assassins Creed',
             'assasins creed', 'assassin\'s creed', 'assassins creed'] , 'assasian' ) 

【讨论】:

    【解决方案2】:

    你可以这样做:

    df = df.replace({word: "Assassin's Creed" for word in list_assasins_creed}, regex=True)
    print(df)
    

    df = df.replace(list_assasins_creed, 'Assassin\'s Creed')
    print(df)
    
                        Q4_1              Q4_2            Q4_3        Q4_4
    0              far cry 4               NaN             NaN         NaN
    1       grand theft auto  Assassin's Creed    call of duty  watch dogs
    2    the elder scrolls v     the witcher 3      dragon age  dark souls
    3              Uncharted      Call of duty        Deadpool     Far cry
    4  Monster Hunter: World  Assassin's Creed  Darksiders III         NaN
    5              Fallout 4               NaN             NaN         NaN
    

    【讨论】:

    • 它有效!比我想象的更容易和简单的解决方案。这也有效:df.replace(list_assasins_creed, 'Assassin\'s Creed', inplace=True)
    猜你喜欢
    • 2023-02-25
    • 2014-04-01
    • 2022-12-23
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多