使用excelwriter添加文本的最简单方法

时间:2016-12-22 07:59:51

标签: python pandas

我有以下代码将数据帧写入excel文件。我想在表格下面添加一个句子' 2016-12-22'中提取的数据。最简单的方法是什么?

import pandas as pd

df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='1')

print('Data extracted on ',pd.datetime.today().strftime('%Y-%m-%d'))

writer.save()

1 个答案:

答案 0 :(得分:4)

你可以这样做:

import pandas as pd

df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='1')

workbook  = writer.book
worksheet = writer.sheets['1']

text = 'Data extracted on ' + pd.datetime.today().strftime('%Y-%m-%d')
worksheet.write(len(df) + 1, 0, text)

writer.save()

输出:

enter image description here

另请参阅:Working with Python Pandas and XlsxWriter