【问题标题】:Replace the names of a list in a dataframe替换数据框中列表的名称
【发布时间】:2019-03-31 06:28:19
【问题描述】:

我正在尝试替换数据框中的列表名称(C 列):

姓名列表(小例子,列表太大):

Jack
Liam
John
Ethan
George
...

小数据框示例:

       A          B                                   C
  French      house                Phone <phone_numbers>
 English      house                 email <adresse_mail>
  French  apartment                      my name is Liam
  French      house                         Hello George
 English  apartment   Ethan, my phone is <phone_numbers>

我的脚本:

import re
import pandas as pd
from pandas import Series

df = pd.read_excel('data_frame.xlsx')
data = Series.to_string(df['C'])

first_names = open('names_list.txt', 'r')
names_read = first_names.readlines()

def names(data):

    names_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, names_read)))
    replace_names = names_regex.sub('<name>', data)

    return replace_names

no_names = names(data)
print(no_names)

作为输出,我有我的整个数据框没有任何修改...

我预计:

                                  C
              Phone <phone_numbers>
               email <adresse_mail>
                  my name is <name>
                       Hello <name>
<name>, my phone is <phone_numbers>

【问题讨论】:

    标签: python pandas list dataframe replace


    【解决方案1】:

    您可以简单地用给定列的值循环替换它们:

    import pandas as pd
    
    l = [
    ['French','house','Phone <phone_numbers>'],
    ['English','house','email <adresse_mail>'],
    ['French','apartment','my name is Liam'],
    ['French','house','Hello George'],
    ['English','apartment','Ethan, my phone is <phone_numbers>']
    ]
    
    names = [
    'Jack',
    'Liam',
    'John',
    'Ethan',
    'George'
    ]
    
    df = pd.DataFrame(l, columns = list('ABC'))
    
    for i in names:
      df.C = df.C.str.replace(i,'<name>')
    
    print(df)
    

    【讨论】:

      【解决方案2】:
      name_list = ['Jack', 'Liam', 'John', 'Ethan']
      mydf = pd.DataFrame({'C': ['Phone <phone_numbers>', 'email <adresse_mail>', 'my name is Liam', 'Hello George', 'Ethan, my phone is <phone_numbers>']})
      

      您可以根据您的名单定义一个正则表达式。然后将这些值与C 列匹配,并将它们替换为apply lambda

      match = mydf.C.str.extractall('(' + '|'.join(name_list) + ')').reset_index().set_index('level_0').rename(columns={0: 'name'})
      mydf = pd.concat([mydf, match], axis=1)
      condition = mydf.match.notnull()
      mydf.loc[condition, 'C'] = mydf[condition].apply(lambda x: x['C'].replace(x['name'], '<name>'), axis=1)
      

      输出

                                           C  match    name
      0                Phone <phone_numbers>    NaN     NaN
      1                 email <adresse_mail>    NaN     NaN
      2                    my name is <name>    0.0    Liam
      3                         Hello <name>    0.0  George
      4  <name>, my phone is <phone_numbers>    0.0   Ethan
      

      【讨论】:

        猜你喜欢
        • 2021-01-03
        • 1970-01-01
        • 2019-10-21
        • 2018-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-03
        相关资源
        最近更新 更多