【发布时间】:2021-06-21 14:07:42
【问题描述】:
我有一张 Excel 表格,我想将其读入 pandas 多索引数据框。复杂之处在于 excel 表包含重复的标题值。阅读 pandas 时,将 .x 添加到第二级标题的末尾而不是第一级。有没有办法必须重命名顶级标题而不是二级标题?
阅读脚本:
from pathlib import Path
import pandas as pd
def main():
xl_file = Path('.') / 'pandasExample.xlsx'
df = pd.read_excel(xl_file, sheet_name='Sheet1', header=[
0, 1], skiprows=[0])
print(df)
if __name__ == '__main__':
main()
输出:
Rectangle Ellipse Rectangle
Width Height a b Width.1 Height.1 Width.2 Height.2
0 10 20 1 2 20 30 40 50
期望的输出:
Rectangle Ellipse Rectangle.1 Rectangle.2
Width Height a b Width Height Width Height
0 10 20 1 2 20 30 40 50
【问题讨论】: