需要Python的pandas的DataFram.Replace帮助

我是 Python 新手,尝试使用 DataFrame.Replace 替换“pandas”字符串,但遇到问题。制表符分隔的文本文件如下:


                            RepStr         KeyStr                ValStr

0                        S Connery      S Connery          Sean Connery

1                       S. Connery     S. Connery          Sean Connery

2                      Connery, S.    Connery, S.          Sean Connery

3        Connery, S; Blofeld, E.S.     Connery, S          Sean Connery

4   Connery; Moore, R.; ES Blofeld        Connery          Sean Connery

5                        R Moore R    Moore Roger                 Moore

6                         R. Moore       R. Moore           Roger Moore

7                        Moore, R.      Moore, R.           Roger Moore

8   ES Blofeld; Connery; Moore, R.     ES Blofeld  Ernst Stavro Blofeld

9            E.S. Blofeld; Connery   E.S. Blofeld  Ernst Stavro Blofeld

10                    E.S. Blofeld   E.S. Blofeld  Ernst Stavro Blofeld

11                         Blofeld        Blofeld  Ernst Stavro Blofeld

12          Blofeld, E.S.; Connery  Blofeld, E.S.  Ernst Stavro Blofeld

我正在尝试根据“KeyStr”列和“ValStr”列中的“Key: Value”对替换“RepStr”列中的匹配项作为变量。它适用于完整细胞的直接值。


import pandas as pd

pipe_data = pd.read_csv('/content/sample_data/NStd.txt', sep='\t')

NStd = pd.DataFrame(pipe_data)

NStd.replace(to_replace={'RepStr':{'KeyStr': 'ValStr'}}, inplace=True)

NStd


如何得到我想要的结果?


慕桂英4014372
浏览 79回答 1
1回答

DIEA

创建一个替换系列,然后与可选参数一起s使用,将 中的值替换为 中的相应值:Series.replaceregex=TrueRepStrss = df.set_index('KeyStr')['ValStr']s.index = r'(?:(?<=;\s)|(?<=^))' + s.index + r'(?=;|$)'df['RepStr'] = df['RepStr'].replace(s, regex=True)0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sean Connery1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sean Connery2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sean Connery3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sean Connery; Ernst Stavro Blofeld4&nbsp; &nbsp; &nbsp;Sean Connery; Roger Moore; Ernst Stavro Blofeld5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R Moore R6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Roger Moore7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Roger Moore8&nbsp; &nbsp; &nbsp;Ernst Stavro Blofeld; Sean Connery; Roger Moore9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ernst Stavro Blofeld; Sean Connery10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ernst Stavro Blofeld11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ernst Stavro Blofeld12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ernst Stavro Blofeld; Sean ConneryName: RepStr, dtype: object
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python