Data Frame to Excel - AttributeError: 'Series' object has no attribute 'columns'
Data Frame to Excel - AttributeError: 'Series' object has no attribute 'columns'
我正在尝试将数据帧写入 Excel 文件。这在过去对我有用,但这次,它给了我一个 AttributeError
.
代码
我有一个名为 data
的数据框,如下所示:
我把它放到这段代码中:
# To find tf-idf values
textVal = data.text.values.astype('str')
vectorizer = TfidfVectorizer()
vectorizer.fit(textVal)
X = vectorizer.transform (textVal).toarray()
names = vectorizer.get_feature_names()
tfidf_dataframe = pd.DataFrame(X, columns = names)
# To print TF-IDF
writer = pd.ExcelWriter('tfidf_test.xlsx', engine='xlsxwriter')
tfidf_dataframe.to_excel(writer)
writer.save()
print("complete")
tfidf_dataframe
看起来像这样:
错误日志
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-9ebe0a5d13a5> in <module>
13
14 # To print TF-IDF
---> 15 tfidf_dataframe.to_excel(writer)
16
17 # To print sentiment analysis
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\core\generic.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes)
2162 from pandas.io.formats.excel import ExcelFormatter
2163
-> 2164 formatter = ExcelFormatter(
2165 df,
2166 na_rep=na_rep,
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\io\formats\excel.py in __init__(self, df, na_rep, float_format, cols, header, index, index_label, merge_cells, inf_rep, style_converter)
403 self.df = df.reindex(columns=cols)
404
--> 405 self.columns = self.df.columns
406 self.float_format = float_format
407 self.index = index
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5268 or name in self._accessors
5269 ):
-> 5270 return object.__getattribute__(self, name)
5271 else:
5272 if self._info_axis._can_hold_identifiers_and_holds_name(name):
AttributeError: 'Series' object has no attribute 'columns'
关于我收到此错误消息的原因有什么想法吗?
您确定您是从 运行 那里得到的吗?你的回溯似乎暗示它不是。
14 # To print TF-IDF
---> 15 tfidf_dataframe.to_excel(writer)
16
17 # To print sentiment analysis
# To print TF-IDF
writer = pd.ExcelWriter('tfidf_test.xlsx', engine='xlsxwriter')
tfidf_dataframe.to_excel(writer)
writer.save()
print("complete")
我相信默认引擎已经是 xlsxwriter
,所以你也可以只做 tfidf_dataframe.to_excel('tfidf_test.xlsx')
我的预感之一可能是,如果此代码存在于模块中,那么您正在积极开发它并且您没有将它重新导入到您的 Jupyter 环境中。
如果是这样,请尝试:
import importlib
importlib.reload(module)
其中 module 是您的代码所在模块的名称。
我正在尝试将数据帧写入 Excel 文件。这在过去对我有用,但这次,它给了我一个 AttributeError
.
代码
我有一个名为 data
的数据框,如下所示:
我把它放到这段代码中:
# To find tf-idf values
textVal = data.text.values.astype('str')
vectorizer = TfidfVectorizer()
vectorizer.fit(textVal)
X = vectorizer.transform (textVal).toarray()
names = vectorizer.get_feature_names()
tfidf_dataframe = pd.DataFrame(X, columns = names)
# To print TF-IDF
writer = pd.ExcelWriter('tfidf_test.xlsx', engine='xlsxwriter')
tfidf_dataframe.to_excel(writer)
writer.save()
print("complete")
tfidf_dataframe
看起来像这样:
错误日志
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-9ebe0a5d13a5> in <module>
13
14 # To print TF-IDF
---> 15 tfidf_dataframe.to_excel(writer)
16
17 # To print sentiment analysis
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\core\generic.py in to_excel(self, excel_writer, sheet_name, na_rep, float_format, columns, header, index, index_label, startrow, startcol, engine, merge_cells, encoding, inf_rep, verbose, freeze_panes)
2162 from pandas.io.formats.excel import ExcelFormatter
2163
-> 2164 formatter = ExcelFormatter(
2165 df,
2166 na_rep=na_rep,
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\io\formats\excel.py in __init__(self, df, na_rep, float_format, cols, header, index, index_label, merge_cells, inf_rep, style_converter)
403 self.df = df.reindex(columns=cols)
404
--> 405 self.columns = self.df.columns
406 self.float_format = float_format
407 self.index = index
c:\users\matay\appdata\local\programs\python\python38\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5268 or name in self._accessors
5269 ):
-> 5270 return object.__getattribute__(self, name)
5271 else:
5272 if self._info_axis._can_hold_identifiers_and_holds_name(name):
AttributeError: 'Series' object has no attribute 'columns'
关于我收到此错误消息的原因有什么想法吗?
您确定您是从 运行 那里得到的吗?你的回溯似乎暗示它不是。
14 # To print TF-IDF
---> 15 tfidf_dataframe.to_excel(writer)
16
17 # To print sentiment analysis
# To print TF-IDF
writer = pd.ExcelWriter('tfidf_test.xlsx', engine='xlsxwriter')
tfidf_dataframe.to_excel(writer)
writer.save()
print("complete")
我相信默认引擎已经是 xlsxwriter
,所以你也可以只做 tfidf_dataframe.to_excel('tfidf_test.xlsx')
我的预感之一可能是,如果此代码存在于模块中,那么您正在积极开发它并且您没有将它重新导入到您的 Jupyter 环境中。
如果是这样,请尝试:
import importlib
importlib.reload(module)
其中 module 是您的代码所在模块的名称。