【问题标题】:Cleaning and vectorization of data数据的清洗和矢量化
【发布时间】:2020-12-24 17:05:46
【问题描述】:

我不知道如何清理和矢量化数据。

train=pd.read_csv('longilati.csv',encoding='mac_roman')
train`
Index(['Comment ', 'Polarity'], dtype='object')

以下数据在我的数据框中:

但是,每当我尝试使用以下代码清理数据时

def remove_pattern(text,pattern):     
    r = re.findall(pattern,text)    
        for i in r:
        text = re.sub(i,"",text)
        return text
train['Tidy'] = np.vectorize(remove_pattern)(train['Comment'],"@[\w]*")
train

我收到此错误KeyError: 'Comment' 这是它的完整堆栈跟踪

KeyError                                  Traceback (most recent call last)
F:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, 
key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

F:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, 
key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return 
self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, 
tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Comment'

【问题讨论】:

    标签: python pandas dataframe data-science data-preprocessing


    【解决方案1】:

    您的列名中comment 后面有空格。 使用

    替换列名
    df.rename({'Comment ':'Comment'}, axis=1, inplace=True)
    

    或使用

    np.vectorize(remove_pattern)(train['Comment '],"@[\w]*")
    

    【讨论】:

    • 我同意这是一个命名问题。如果它在原始来源中,我建议尽可能在那里修复它,否则在处理中进行重命名是可以接受的。
    • 非常感谢 @Smurphy0000 它帮助了我...我是数据科学新手,所以不知道这种类型的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2019-03-15
    • 2021-11-21
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多