【发布时间】:2021-02-07 22:21:15
【问题描述】:
假设我有一个 excel 文件 data.xlsx,其中包含多个工作表:sheet1, sheet2, sheet3, etc.
import pandas as pd
xls = pd.ExcelFile('data.xlsx')
for sheet in xls.sheet_names:
df = pd.read_excel('data.xlsx', sheet_name=sheet)
对于每个工作表,它具有相同的列名:'ISIN', 'Coupon', 'Issue Date', 'Maturity Date'。
现在我需要迭代读取所有工作表并用date_list的元素修改列名ISIN,即date_list = ['2021-01', '2021-02', '2021-03', ...],然后保存为excel文件。
最终文件的sheet1 的列名将是['2021-01', 'Coupon', 'Issue Date', 'Maturity Date'],sheet2 的将是['2021-02', 'Coupon', 'Issue Date', 'Maturity Date'],等等。
我如何在 Python 中做到这一点?谢谢。
【问题讨论】:
标签: python-3.x pandas dataframe xlsxwriter