【问题标题】:alteration of use values in dictionary to replace values in column更改字典中的使用值以替换列中的值
【发布时间】:2020-01-07 18:23:22
【问题描述】:

这个问题是use values in dictionary to replace values in column 的后续问题。以下数据框是对 use values in dictionary to replace values in column 中使用的数据框和字典

import pandas as pd
dff= pd.DataFrame({'Data':['Hey BL171111 this is 123456 Jonny Good (511)2321134 1A1619',
                              'This is Jonny Good at 511-233-1137 A-1-24',
                          'Jonny Good and go way back in 03-15-2001',
                                  'Wow AL-17-1111 that is Alice Wonderland A999b dont 5643389 cool but NOT 1-2-2001',
                                  'Yes hi: Mick Mouse 1A25629Q88 or ',
                            'Bye Mick Mouse A13B ok was seen on 19S-9'], 
                          'E_ID': ['E11','E11','E11', 'E22', 'E33', 'E33'],
                           'N_ID' : ['111', '112', '113', '211', '311', '312'],
                           'Name' : ['JONNY GOOD', 'JONNY GOOD', 'JONNY GOOD', 
                                      'ALICE WONDERLAND',
                                      'MICK MOUSE', 'MICK MOUSE'],        
                          })

这是新词典

dd = {'E11': ['123456',
  'Jonny',
  'Good',
  '(511)2321134',
  '1A1619',
   'Jonny',
  'Good',
  '511-233-1137',
   'BL171111',
   'A-1-24',
   'Jonny',
  'Good',
  '03-15-2001'],

'E22': ['Alice',
  'Wonderland',
    'AL-17-1111',
  'A999b',
  '5643389',
  '1-2-2001'],

'E33': ['Mick', 
        'Mouse',
  '1A25629Q88',
        'Mick', 
        'Mouse',
  'A13B',
  '19S-9']}

当我应用取自 use values in dictionary to replace values in column 的答案时,它似乎适用于我在下面显示的内容。也就是说,来自dd 的所有值,包括我包含的新值,例如'(511)2321134': '@@@' 正在与对应的 @@@ 配对

   d2 = {k: {x: '@@@' for x in v} for k, v in dd.items()}
   d2
   {'E11': {'(511)2321134': '@@@',
  '03-15-2001': '@@@',
  '123456': '@@@',
  '1A1619': '@@@',
  '511-233-1137': '@@@',
  'A-1-24': '@@@',
  'BL171111': '@@@',
  'Good': '@@@',
  'Jonny': '@@@'},
 'E22': {'1-2-2001': '@@@',
  '5643389': '@@@',
  'A999b': '@@@',
  'AL-17-1111': '@@@',
  'Alice': '@@@',
  'Wonderland': '@@@'},
 'E33': {'19S-9': '@@@',
  '1A25629Q88': '@@@',
  'A13B': '@@@',
  'Mick': '@@@',
  'Mouse': '@@@'}}

我还使用下面的代码,取自use values in dictionary to replace values in column

dff['New_Data'] = (dff.pivot(columns='E_ID', values='Data')
                .replace(d2, regex=True).bfill(1).iloc[:,0])
dff



        New_Data
0       Hey @@@ this is @@@ @@@ @@@ @@@ (511)2321134 @@@
1       This is @@@ @@@ @@@ at @@@ @@@
2       @@@ @@@ @@@ and go way back in @@@
3       Wow @@@ that is @@@ @@@ @@@ @@@ dont @@@ cool but NOT @@@
4       Yes hi: @@@ @@@ @@@ @@@ or
5       Bye @@@ @@@ @@@ @@@ ok was seen on @@@

但是(511)2321134 没有变成@@@。不知道为什么?

理想情况下,我想要以下

        New_Data
0       Hey @@@ this is @@@ @@@ @@@ @@@ @@@ @@@
1       This is @@@ @@@ @@@ at @@@ @@@
2       @@@ @@@ @@@ and go way back in @@@
3       Wow @@@ that is @@@ @@@ @@@ @@@ dont @@@ cool but NOT @@@
4       Yes hi: @@@ @@@ @@@ @@@ or
5       Bye @@@ @@@ @@@ @@@ ok was seen on @@@

好像

d2 = {k: {x: '@@@' for x in v} for k, v in dd.items()} 运行良好

所以我假设问题出在

(dff.pivot(columns='E_ID', values='Data')
                    .replace(d2, regex=True).bfill(1).iloc[:,0])

如何更改代码以将 (511)2321134 也变为 @@@ ?

【问题讨论】:

    标签: python-3.x string pandas dictionary replace


    【解决方案1】:

    ( 和) 是正则表达式中的特殊字符,因此您需要将它们在(511)2321134 中转义为\\(511\\)2321134。在另一个答案中使用我相同的解决方案,只需要额外的maketrans 和translate

    tr = str.maketrans({"(": "\(", ")": "\)"})
    d2 = {k: {x.translate(tr): '@@@' for x in v} for k, v in dd.items()}
    
    Out[864]:
    {'E11': {'123456': '@@@',
      'Jonny': '@@@',
      'Good': '@@@',
      '\\(511\\)2321134': '@@@',
      '1A1619': '@@@',
      '511-233-1137': '@@@',
      'BL171111': '@@@',
      'A-1-24': '@@@',
      '03-15-2001': '@@@'},
     'E22': {'Alice': '@@@',
      'Wonderland': '@@@',
      'AL-17-1111': '@@@',
      'A999b': '@@@',
      '5643389': '@@@',
      '1-2-2001': '@@@'},
     'E33': {'Mick': '@@@',
      'Mouse': '@@@',
      '1A25629Q88': '@@@',
      'A13B': '@@@',
      '19S-9': '@@@'}}
    
    (dff.pivot(columns='E_ID', values='Data')
        .replace(d2, regex=True).bfill(1).iloc[:,0])
    
    Out[867]:
    0    Hey @@@ this is @@@ @@@ @@@ @@@ @@@
    1    This is @@@ @@@ at @@@ @@@
    2    @@@ @@@ and go way back in @@@
    3    Wow @@@ that is @@@ @@@ @@@ dont @@@ cool but NOT @@@
    4    Yes hi: @@@ @@@ @@@ or
    5    Bye @@@ @@@ @@@ ok was seen on @@@
    Name: E11, dtype: object
    

    【讨论】:

      猜你喜欢
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 2018-02-08
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      相关资源
      最近更新 更多