【问题标题】:Remove for loop in python pandas find and replace text删除python pandas中的for循环查找和替换文本
【发布时间】:2017-10-21 06:07:03
【问题描述】:

我有 2 个熊猫数据框。我想在 2 个数据帧之间进行查找和替换。在df_find 数据帧的 current_title 列中,我想在每一行中搜索df_replace 数据帧中“keywrod”列中出现的任何值,如果找到,将其替换为“keywordlength”中的相应值' 列。

我已经能够摆脱df_find 数据帧的循环,因为我需要使用str.replace 迭代此数据帧中的每一行,str.replacereplace 函数的矢量化形式。

在我的情况下,性能很重要,因为两个数据帧都运行在 GB 中。所以,我想在这里摆脱df_replace 的循环,并使用任何其他有效的方法来遍历df_replace 数据帧的所有行。

import pandas as pd
df_find = pd.read_csv("input_find.csv")
df_replace = pd.read_csv("input_replace.csv")

#replace
for i,j in zip(df_replace.keyword,df_replace.keywordLength):
    df_find.current_title=df_find.current_title.str.replace(i,j,case=False)

df_replace 该数据框包含我们查找和替换所需的数据

keyword       keywordLength
IT Manager    ##10##
Sales Manager ##13##
IT Analyst    ##12##
Store Manager ##13##

df_find 是我们需要进行转换的地方。

在执行查找和替换代码之前:

current_title
I have been working here as a store manager since after I passed from college
I am sales manager and primarily work in the ASEAN region. My primary rolw is to bring new customers.
I initially joined as a IT analyst and because of my sheer drive and dedication, I was promoted to IT manager position within 3 years

通过上述代码执行查找和替换后

current_title
I have been working here as a ##13## since after I passed from college
I am ##13## and primarily work in the ASEAN region. My primary rolw is to bring new customers.
I initially joined as a ##12## and because of my sheer drive and dedication, I was promoted to ##10## position within 3 years

我将永远感激不尽!谢谢

【问题讨论】:

  • 匹配的值是完全匹配,还是只有子字符串匹配?如果有多个匹配项怎么办?你只参加第一场比赛吗?
  • 替换所有匹配项。完全匹配。
  • 查看正则表达式和re.sub。您可以将文件作为文本读取,将要替换的内容替换为正则表达式,然后以 csv 格式打开。
  • str.replace 是 re-sub 的矢量化实现。它对整列而不是单行执行操作。

标签: python performance pandas for-loop vectorization


【解决方案1】:

如果我对您的理解正确,您应该能够对您的数据集(与其他几行)进行相对简单的合并并获得所需的结果。

没有你的数据集,我只是自己编的。下面的代码可能会更优雅一些,但它可以让你在四行中到达你需要的地方,最重要的是 - 没有循环:

设置:

df_find = pd.DataFrame({
            'current_title':['a','a','b','c','b','c','b','a'],
            'other':['this','is','just','a','bunch','of','random','words']
        })

df_replace = pd.DataFrame({'keyword':['a','c'], 'keywordlength':['x','z']})

代码:

# This line is to simply re-sort at the end of the code.  Someone with more experience can probably bypass this step.
df_find['idx'] = df_find.index

# Merge together the two data sets based on matching the "current_title" and the "keyword"
dfx = df_find.merge(df_replace, left_on = 'current_title', right_on = 'keyword', how = 'outer').drop('keyword', 1)

# Now, copy the non-null "keywordlength" values to "current_title"
dfx.loc[dfx['keywordlength'].notnull(), 'current_title'] = dfx.loc[dfx['keywordlength'].notnull(), 'keywordlength']

# Clean up by dropping the unnecessary columns and resort based on the first line above.
df_find = dfx.sort_values('idx').drop(['keywordlength','idx'], 1)

输出:

  current_title   other
0             x    this
1             x      is
3             b    just
6             z       a
4             b   bunch
7             z      of
5             b  random
2             x   words

【讨论】:

  • 这不是我要找的。我添加了之前和之后的示例以使其更加清晰。谢谢!
  • 好吧,这就是为什么我问他们是完全匹配还是子字符串匹配......无论如何,这突出了发布数据集的重要性。
  • 完全匹配。我们用关键字长度替换整个关键字。 for 循环中的代码就是这样做的。不知道你所说的“完全匹配”是不是别的意思,但我是按照字面意思来的。
  • 你能帮我吗?
猜你喜欢
  • 2014-06-28
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
  • 2017-01-10
相关资源
最近更新 更多