Python 将数据框导出到excel会导致类型错误
我正在尝试将数据框从pandas导出到excel,具体操作如下:Python 将数据框导出到excel会导致类型错误,python,excel,pandas,Python,Excel,Pandas,我正在尝试将数据框从pandas导出到excel,具体操作如下: writer = pd.io.excel.ExcelWriter(args.out_file, engine='xlsxwriter', options={'constant_memory': True}) summary_data.to_excel(writer, sheet_name='summary', na_rep='NA', index=False) 但我得到的信息是: "cannot convert the seri
writer = pd.io.excel.ExcelWriter(args.out_file, engine='xlsxwriter', options={'constant_memory': True})
summary_data.to_excel(writer, sheet_name='summary', na_rep='NA', index=False)
但我得到的信息是:
"cannot convert the series to {0}".format(str(converter)))
TypeError: cannot convert the series to <type 'float'>
count应该是int,但我可以看到它在这里被视为字符串,可能是因为它是从另一个数据框提取的?。\u excel只接受类型为:object的列。解决此问题的快速方法是,在使用以下命令写入之前,将所有列强制为对象类型:
summary_data = summary_data.astype(object)
然后,您可以编写它而不会崩溃:
summary_data.to_excel(writer, sheet_name='summary', na_rep='NA', index=False)
这里需要做一些模切,因为在某些情况下,我必须将列复制为对象类型。维尔德。另一种选择是删除有问题的列 对于字符串类,您应该只有
object
d类型。您是如何生成数据的?正确,但我可以看到计数有对象数据类型,这是可疑的。您为什么要迭代????这些都是简单的矢量化calcsWell,实际上这不是我的代码,但我使用的是一个工具,它实现了这些错误,你认为不需要第一个for循环吗?我认为迭代是因为数据包含不同的数据[sample],将其视为具有不同sheetsSide问题的excel工作簿:在constant_memory
mode XlsxWriter中,XlsxWriter要求以行x列顺序写入数据,但excel writer以列x行顺序工作。因此,我想知道常量_memory
选项是否真的被传递了,或者数据帧是否真的被正确写入了。
summary_data = summary_data.astype(object)
summary_data.to_excel(writer, sheet_name='summary', na_rep='NA', index=False)