Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 更改字典中的使用值以替换列中的值_Python 3.x_String_Pandas_Dictionary_Replace - Fatal编程技术网

Python 3.x 更改字典中的使用值以替换列中的值

Python 3.x 更改字典中的使用值以替换列中的值,python-3.x,string,pandas,dictionary,replace,Python 3.x,String,Pandas,Dictionary,Replace,这个问题是一个后续问题。以下数据帧是对 数据库中使用的数据帧和字典 这是新字典 dd = {'E11': ['123456', 'Jonny', 'Good', '(511)2321134', '1A1619', 'Jonny', 'Good', '511-233-1137', 'BL171111', 'A-1-24', 'Jonny', 'Good', '03-15-2001'], 'E22': ['Alice', 'Wonderl

这个问题是一个后续问题。以下数据帧是对 数据库中使用的数据帧和字典

这是新字典

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']}
当我应用从中得到的答案时,它似乎对我下面展示的内容很有效。也就是说,
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': '@@@'}}
我还使用以下代码,这些代码取自

但是
(511)2321134
未转换为
@@@@code>。我不知道为什么

理想情况下,我希望如下

        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
转换为
@@@code>

是正则表达式中的特殊字符,因此需要将它们在
(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
是正则表达式中的特殊字符,因此需要将它们在
(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
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