python - 无法使用 Pandas 写入具有有效日期格式的 Excel 工作表

标签 python excel pandas dataframe

背景:

1、使用Python执行SQL并使用Pandas将输出结果保存为DataFrame格式。

2,将输出作为一个新工作表附加到我现有的 excel 文件中。

下面是我的代码:

from pandas import ExcelWriter

sql_20 = ''''''

db = cx_Oracle.connect('*****', '*******', '**********')
conn = db.cursor()

conn.execute(sql_20)
df = pandas.read_sql_query(sql_20,db)
print(df)

with ExcelWriter('GUCS6J-Job Data.xlsx', mode='a') as writer:
    df.to_excel(writer, sheet_name=str(20))
writer.save()

数据库:甲骨文

A列数据类型:日期

Oracle 的 SQL 结果:

enter image description here

来自 Python 的 DataFrame 打印:

enter image description here

到目前为止一切顺利


但我在 excel 工作表中得到的是:

enter image description here

enter image description here

我很好奇为什么我得到'##################' 并且这个单元格的值在excel 中是-693594

希望能得到和excel文件中DataFrame打印一样的结果。

最佳答案

请引用这两个链接看是否能解决您的问题 https://xlsxwriter.readthedocs.io/example_pandas_datetime.html Python pandas dataframe "Date" Index different format in xlsx and csv

我认为您需要在 ExcelWriter() 中添加 date_format

关于python - 无法使用 Pandas 写入具有有效日期格式的 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634261/

相关文章:

python - 如何在 numpy 数组中应用条件语句?

python - 根据没有名称值的列过滤数据框

python - 如何使用 https 而不是 http 启动 Dockerized Python Flask 应用程序?

Excel VBA 如何使用超链接在工作表之间将信息从一个单元格复制到另一个单元格

python - 如何检查 python pandas 中列内容的数据类型?

VBA - "Long"如何在 VBA 中工作 - 玩溢出

excel - 如何在Excel中将字符串与文本和数字分开

python - 如何从与 Bokeh 的 CustomJS 函数的局部变量同步的 ColumnDataSource 对象获取数据?

python - Django 'str' 对象在 rest_framework 中没有属性 'values'

python - if 命令与 python os 路径模块