【发布时间】:2019-07-10 19:27:33
【问题描述】:
我正在尝试从列中的所有行中删除标点符号。所有这些行都包含字符串数据。我尝试了几个正则表达式,但没有一个工作。谁能告诉我这个语法的问题出在哪里?
for i in range(0, 3847):
#Remove punctuation
text = re.sub(r'[^\w\s]','',dataset['abstract1'][i])
这是我得到的错误:
4 #Remove punctuations
----> 5 text = re.sub('[^\w\s]','',dataset['abstract1'][i])
6
7 #Convert to lowercase
G:\Anaconda3\lib\site-packages\pandas\core\series.py in
__getitem__(self, key)
866 key = com.apply_if_callable(key, self)
867 try:
--> 868 result = self.index.get_value(self, key)
869
870 if not is_scalar(result):
G:\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
4373 try:
4374 return self._engine.get_value(s, k,
-> 4375 tz=getattr(series.dtype, 'tz', None))
4376 except KeyError as e1:
4377 if len(self) > 0 and (self.holds_integer() or self.is_boolean()):
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in
pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in
pandas._libs.hashtable.Int64HashTable.get_item()
【问题讨论】:
-
您似乎遗漏了错误消息中说明密钥实际是什么的部分,因此很难说。但我猜问题是
dataset没有名为abstract1的键。
标签: python regex pandas keyerror