我描述了一个类,该类将服务器中的数据加载到数据框中,然后对其进行处理。这是我的代码(不包括导入库):
class Save(Data):
def __init__(self, server, database, username, driver, group=None):
super().__init__(server, database, username, driver)
self.group = group
def get_all_goods(self):
goods_table = pd.read_sql_query(f''' SELECT [p_1], [p_2]
FROM [table] WHERE [group] = '{self.group}' ''',
self.hndl)
return goods_table
def data_preprocessing(self):
data_prepared = self.get_all_goods()
data_prepared['desc'] = data_prepared[['p_1', 'p_2']].apply(lambda x: ' '.join(x), axis=1)
return data_prepared
@staticmethod
def data_cleaning(str):
words = []
str = re.sub(r"(\w*(\.\w*))", ' ', str)
str = re.sub(r"\d*\_\d*", ' ', str)
for i in re.split('[;,.,\n,\s,:,-,+,(,),=,/,«,»,@,!,?,",_,*]',str):
if len(i) > 1:
words.append(i)
return words
def data_morphy(self, text):
morph = pymorphy2.MorphAnalyzer()
tokens = [morph.parse(token)[0].normal_form for token in self.data_cleaning(text) if token != ' ']
tokens = list(dict.fromkeys(tokens))
text = ' '.join(tokens)
return text
def data_final(self):
data_final = self.data_preprocessing()
data_final['desc'] = data_final['desc'].apply(lambda x: self.data_morphy(x))
return data_final
但是,当我运行代码时,它无法完成其工作,我等待了半个小时,但代码未执行。但是方法 get_all_goods 和 data_preprocessing 效果很好。我在哪里弄错了?
编辑:
当我使用cls.data_final()
时出现错误:
Traceback (most recent call last):
File "C:\Program Files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-8-66905faafdf3>", line 103, in <module>
cls.data_final()
File "<ipython-input-8-66905faafdf3>", line 68, in data_final
data_final['desc'] = data_final['desc'].apply(lambda x: self.data_morphy(x))
TypeError: 'method' object is not subscriptable
答案 0 :(得分:0)
名称data_final
作为数据框的名称和函数的名称被共享。当尝试将data_final['desc']
用作熊猫数据框时,代码认为您正在尝试调用data_final()
方法。这就是为什么您得到TypeError: 'method' object is not subscriptable
解决方案-将熊猫框架的名称更改为非函数名称,例如:
def data_final(self):
dont_name_your_variables_the_same_as_your_functions = self.data_preprocessing()
dont_name_your_variables_the_same_as_your_functions['desc'] = dont_name_your_variables_the_same_as_your_functions['desc'].apply(lambda x: self.data_morphy(x))
return dont_name_your_variables_the_same_as_your_functions
:)