Pandas applymap内存错误

时间:2018-06-18 08:47:22

标签: python pandas xlwings

我正在尝试将datetime的所有实例更改为字符串,并将我的数据框导出为ex​​cel。数据框可能有20,000到50,000个条目。这是我目前的代码:

wb = xw.Book('filelname.xlsx')
sht = wb.sheets['FNC']
data = wb.sheets['Data']
smdata = wb.sheets['Social']

df_sm_temp = sm_all

df_sm_temp['Date'] = df_sm_temp['Date'].apply(lambda x: str(x) if isinstance(x, datetime.time) else x)
df_sm_temp['Added'] = df_sm_temp['Added'].apply(lambda x: str(x) if isinstance(x, datetime.time) else x)
df_sm_temp['Updated'] = df_sm_temp['Updated'].apply(lambda x: str(x) if isinstance(x, datetime.time) else x)

 smdata.range('SM_ALL').value = df_sm_temp

尝试运行时,我得到一个MemoryError。任何帮助将不胜感激!

以下是数据集的一小部分示例:https://www.sendspace.com/file/jxhmx2

由于

1 个答案:

答案 0 :(得分:2)

实际上您不需要转换数据本身,只需将datetime_formatxlsxwriter引擎结合使用:

演示:

writer = pd.ExcelWriter('/path/to/result.xlsx', engine='xlsxwriter', 
                        date_format='mm/dd/yyyy', datetime_format='mm/dd/yyyy')

df.to_excel(writer)