我有一个有1列的df
List
0 What are you trying to achieve
1 What is your purpose right here
2 When students don’t have a proper foundation
3 I am going to DESCRIBE a sunset
Run Code Online (Sandbox Code Playgroud)
我有其他数据帧df2
它有2列
original correct
0 are were
1 sunset sunrise
2 I we
3 right correct
4 is was
Run Code Online (Sandbox Code Playgroud)
我想在我的df中替换这样的单词,这些单词出现在original我的df2列中,并替换为correct列中的相应单词.并将新字符串存储在其他数据帧中df_new
是否可以不使用循环和迭代,只使用普通的熊猫概念?
即我df_new应该包含.
List
0 What were you trying to achieve
1 What was your purpose correct here
2 When students don’t have a proper foundation
3 we am going to DESCRIBE a sunrise
Run Code Online (Sandbox Code Playgroud)
这也只是一个测试示例,我可能会拥有df数百万行字符串,所以我的df2,我能继续进行的最有效的解决方案路径是什么?
许多可能的解决方案之一:
\n\nIn [371]: boundary = r'\\b'\n ...:\n ...: df.List.replace((boundary + df2.orignal + boundary).values.tolist(),\n ...: df2.correct.values.tolist(),\n ...: regex=True)\n ...:\nOut[371]:\n0 What were you trying to achieve\n1 What was your purpose correct here\n2 When students don\xe2\x80\x99t have a proper foundation\n3 we am going to DESCRIBE a sunrise\nName: List, dtype: object\nRun Code Online (Sandbox Code Playgroud)\n