【问题标题】:Replace string in the same column in Series pandas替换系列熊猫中同一列中的字符串
【发布时间】:2021-05-15 17:29:12
【问题描述】:

事情可能乍一看并不容易理解,但要循序渐进......

这是我大约 50000 行数据框 df_answers_clean 的开始

使用len(set(df_answers_clean['Race'])) 后,我得到了 98 个独特的职位,这对于我未来的分类来说太多了。 我举了一个例子——他们的 25 个职位列表如下:

['Native American, Pacific Islander, or Indigenous Australian; South Asian; White or of European descent', 'Hispanic or Latino/Latina; South Asian', 'East Asian; Hispanic or Latino/Latina',
 'East Asian', 'Black or of African descent; East Asian; South Asian; White or of European descent',
 'Black or of African descent; East Asian; Hispanic or Latino/Latina; Middle Eastern; Native American, Pacific Islander, or Indigenous Australian; South Asian; White or of European descent',
 'Hispanic or Latino/Latina; White or of European descent', 'White or of European descent; I prefer not to say', 'South Asian; White or of European descent', 'White or of European descent',
 'Hispanic or Latino/Latina', 'Black or of African descent; I don’t know; I prefer not to say',
 'Native American, Pacific Islander, or Indigenous Australian; White or of European descent; I don’t know', 'East Asian; White or of European descent; I don’t know', 'Native American, Pacific Islander, or Indigenous Australian', 'South Asian; White or of European descent; I don’t know',
 'Black or of African descent; Middle Eastern; White or of European descent; I don’t know',
 'Hispanic or Latino/Latina; Middle Eastern; White or of European descent',
 'Middle Eastern; White or of European descent',
 'Middle Eastern; South Asian']

我用很多行代码清理了这个烂摊子:

df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^Black or of African descent[\s\S]*', 'Black or of African descent')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^East Asian[\s\S]*', 'East Asian')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^Hispanic or Latino/Latina[\s\S]*', 'Hispanic or Latino/Latina')
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^Middle Eastern[\s\S]*', 'Middle Eastern')
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^Native American, Pacific Islander, or Indigenous Australian[\s\S]*', 'Native American, Pacific Islander, or Indigenous Australian')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^South Asian[\s\S]*', 'South Asian')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^White or of European descent[\s\S]*', 'White or of European descent')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^I don’t know[\s\S]*', 'No data')    
df_answers_clean['Race'] = df_answers_clean['Race'].str.replace('^I prefer not to say[\s\S]*', 'No data')

结果是唯一的组,现在才对后面的分类任务有用:

{'Black or of African descent',
 'East Asian',
 'Hispanic or Latino/Latina',
 'Middle Eastern',
 'Native American, Pacific Islander, or Indigenous Australian',
 'No data',
 'South Asian',
 'White or of European descent'}

正如我所说 - 它可以工作,但许多重复的代码行不实用/不实用。

我这样做的另一个想法是列出我的最终结果 (race_names_change) 并通过 for-loop 放置所有内容:

race_names_change = ['Black or of African descent', 'East Asian', 'Hispanic or Latino/Latina', 'Middle Eastern', 'South Asian', 'Native American, Pacific Islander, or Indigenous Australian', 'White or of European descent']

for i in race_names_change:
    replace_string = str('^'+ i +'[\s\S]*')
    df_answers_clean['Race'].str.replace('replace_string', i, regex=True)

但不幸的是它不起作用 - 列表与开头相同(98个位置)。

也许循环代码或任何其他方式(映射、应用)有问题?

感谢您的建议。

【问题讨论】:

    标签: python regex pandas replace


    【解决方案1】:

    如果您可以创建具有所需正则表达式模式和相应输出的字典,那么您可以简单地使用pd.Series.replace()

    d = {
        'pattern1':'output1',
        'pattern2':'output2'
        }
    
    df_answers_clean['Race'].replace(d, regex=True)
    

    注意,pd.Series.str.replace() 与 pd.Series.replace() 不同


    试试这个 -

    race_names_change = ['Black or of African descent', 'East Asian', 'Hispanic or Latino/Latina', 'Middle Eastern', 'South Asian', 'Native American, Pacific Islander, or Indigenous Australian', 'White or of European descent']
    
    d = {}
    for i in race_names_change:
        replace_string = str('^'+ i +'[\s\S]*')
        replace_string
        d.update({replace_string:i})
        
    df_answers_clean['Race'].replace(d, regex=True)
    

    【讨论】:

    • 它替换了我的 Python Notebook 中结果列表中的所有数据,但是当我在下一行中写入 len(set(df_answers_clean['Race'])) 时,我得到的列表与开头相同(98 个职位)。听起来它不是“就地”更新,而是暂时的。
    • 不是inplace,pandas函数不是这样工作的,你可以通过df_answers_clean['Race'] = df_answers_clean['Race'].replace(d, regex=True)将其设置为inplace
    • 现在可以了。正如我所料 - 在该行的开头没有比较 df_answers_clean['Race'] =。
    • 对不起,我不明白你的意思
    猜你喜欢
    • 2020-03-07
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 2020-04-08
    • 2019-03-04
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多