【发布时间】:2019-03-13 13:49:34
【问题描述】:
我正在尝试使用它们可能包含的一些正则表达式字符串按主题标记一些关键字。理想情况下,这将在数据框中附加一个“类别”列,其中包含它所属的标签或“其他”(如果没有找到)。
我试图标记的数据基本上如下所示:
| Keyword | Volume |
|:-----------|------------:|
| audi specs | 4000 |
| bmw width | 170 |
| a45 bhp | 30 |
| a1 length | 210 |
| alfa co2 | 10 |
我目前得到的代码是:
import pandas as pd
import numpy as np
import re
from IPython.display import display
df = pd.read_csv("make-model-keywords.csv")
df = pd.DataFrame(df, columns=['Keyword', 'Volume','Keyword Difficulty','CPC (USD)', 'SERP Features'])
tags = [
{
"name": "Dimensions",
"regex": "dimension|width|height|length|size"
},
{
"name": "MPG",
"regex": "mpg|co2|emission|consumption|running|economy|fuel"
},
{
"name": "Specs",
"regex": "spec|specification|torque|bhp|weight|rpm|62|mph|kmh"
}
]
def basic_tagging(string, tags):
for tag in tags:
if re.match(tag['regex'], row['Keyword']):
return tag['name']
else:
return "other"
df['Category'] = df.apply(lambda x: basic_tagging(x['Keyword'], tags), axis=1)
但是它给了我以下错误:
---------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-71-31890ef48022> in <module>()
----> 1 df['Category'] = df.apply(lambda row: basic_tagging(row['Keyword'], tags), axis=1)
2 df.head()
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
6012 args=args,
6013 kwds=kwds)
-> 6014 return op.get_result()
6015
6016 def applymap(self, func):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in get_result(self)
140 return self.apply_raw()
141
--> 142 return self.apply_standard()
143
144 def apply_empty_result(self):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self)
246
247 # compute the result using the series generator
--> 248 self.apply_series_generator()
249
250 # wrap results
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self)
275 try:
276 for i, v in enumerate(series_gen):
--> 277 results[i] = self.f(v)
278 keys.append(v.name)
279 except Exception as e:
<ipython-input-71-31890ef48022> in <lambda>(row)
----> 1 df['Category'] = df.apply(lambda row: basic_tagging(row['Keyword'], tags), axis=1)
2 df.head()
<ipython-input-68-1867110ca579> in basic_tagging(string, tags)
1 def basic_tagging(string, tags):
2 for tag in tags:
----> 3 if re.match(tag['regex'], row['Keyword']):
4 return tag['name']
5 else:
NameError: ("name 'row' is not defined", 'occurred at index 0')
我是否遗漏了一些显而易见的东西?
【问题讨论】:
-
我认为您没有创建名为“行”的表或数组
标签: python-3.x pandas jupyter-notebook