gpt4 book ai didi

python - Pandas Excelwriter 内存错误

转载 作者:行者123 更新时间:2023-12-02 00:58:04 29 4
gpt4 key购买 nike

import pandas

#Ignores a value is trying to be set on a copy of a slice from a DataFrame(side note if I shouldn't be doing this please let me know too)
pandas.options.mode.chained_assignment = None

#opens file
f = pandas.read_excel('.../foo.xlsx', sheetname=0)

#sort by header_number and adjust index
f = f.sort(columns=['FY15'],ascending=[0])
f.index = range(0,len(f))

#create field column
f['AB_Test'] = ''

#A/B Iteration
for i, row in enumerate(f['AB_Test']):

if i % 2 == 0:
f['AB_Test'][i] = 'A'
else:
f['AB_Test'][i] = 'B'

print f

#output to Excel
writer = pandas.ExcelWriter('output.xlsx')
f.to_excel(writer,'Sheet1')
writer.save()

f.close

收到错误

Traceback (most recent call last):
File "C:/Users/jmhall/PycharmProjects/untitled/abtest", line 33, in <module>
writer.save()
File "C:\Python27\lib\site-packages\pandas\io\excel.py", line 651, in save
return self.book.save(self.path)
File "C:\Python27\lib\site-packages\openpyxl\workbook.py", line 265, in save
save_workbook(self, filename)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 186, in save_workbook
writer = ExcelWriter(workbook)
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 71, in __init__
self.style_writer = StyleWriter(self.workbook)
File "C:\Python27\lib\site-packages\openpyxl\writer\styles.py", line 35, in __init__
self._style_list = self._get_style_list(workbook)
File "C:\Python27\lib\site-packages\openpyxl\writer\styles.py", line 43, in _get_style_list
uniqueStyles = dict((id(style), style) for style in worksheet._styles.values()).values()
MemoryError

Process finished with exit code 1

我从我找到的不同解决方案运行下面的代码并得到下面的结果。

代码

pandas.__version__
pandas.show_versions(as_json=False)

结果

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.10.final.0
python-bits: 32
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None

pandas: 0.16.2
nose: None
Cython: None
numpy: 1.9.2
scipy: None
statsmodels: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.4.2
pytz: 2015.4
bottleneck: None
tables: None
numexpr: None
matplotlib: None
openpyxl: 1.8.6
xlrd: 0.9.4
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None

最佳答案

会不会是你输入的文件太大了? (根据 MemoryError 判断。)在这种情况下,您可以尝试将数据分成更小的 block ,而不是尝试一次将其全部写入 .xlsx 文件。

关于python - Pandas Excelwriter 内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32238031/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com