-1

背景

我有一个数据集 df,我想将字符串替换为“已连接到通话(音频、视频或屏幕共享)”,并将“已结束通话”替换为“已结束”

Connect                                                               End

Connected to call (audio, video or screen sharing)                    3/3/2020 2:00:01 PM
Ended call                                                            3/3/2020 2:05:00 PM

期望的输出:

Connect                                                              End

Connected                                                            3/3/2020 2:00:01 PM
Ended                                                                3/3/2020 2:05:00 PM

我试过的:

df1 = df["Connect"] = df["Connect"].replace(Connected to call (audio, video, or screen sharing),      "Connected")

此外,如果字符串位于多列中,我将如何替换它们?连接和结束?(如上图)

任何建议表示赞赏。

4

2 回答 2

1

numpy.select

# sample date
s = """Connect|End
Connected to call (audio, video or screen sharing)|3/3/2020 2:00:01 PM
Ended call|3/3/2020 2:05:00 PM"""
df = pd.read_csv(StringIO(s), sep='|')

# numpy.select with your conditions, choices and default value if condition is not met
df['Connect'] = np.select([df['Connect'].str.contains('Connected'), df['Connect'].str.contains('Ended')],
                          ['Connected', 'Ended'], df['Connect'])


     Connect                  End
0  Connected  3/3/2020 2:00:01 PM
1      Ended  3/3/2020 2:05:00 PM
于 2020-04-23T20:29:05.870 回答
1

您需要在更换时parantheses逃脱\。这就是造成问题的原因。

所以做这样的事情:

In [133]: df.Connect.str.replace("Connected to call \(audio, video or screen sharing\)", 'Connected')                                                                                                       
Out[133]: 
0     Connected
1    Ended call
Name: Connect, dtype: object

对于所有替换,您可以执行以下操作:

In [142]: replacements= {'Connect' : {"Connected to call \(audio, video or screen sharing\)" : 'Connected', 'Ended call': 'Ended'}}                                                                         

In [143]: df.replace(replacements, regex=True, inplace=True)                                                                                                                                                

In [144]: df                                                                                                                                                                                                
Out[144]: 
     Connect
0  Connected
1      Ended
于 2020-04-23T20:30:11.020 回答