I have an excel sheet that I would like to read into a pandas multiindex dataframe. The complication is that the excel sheet contains duplicate header values. When reading pandas is adding a .x to the end of the second level headers instead of the first. Is there a way to have to rename the top level header instead of the second level?
Read Script:
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()
The output:
Rectangle Ellipse Rectangle
Width Height a b Width.1 Height.1 Width.2 Height.2
0 10 20 1 2 20 30 40 50
Desired output:
Rectangle Ellipse Rectangle.1 Rectangle.2
Width Height a b Width Height Width Height
0 10 20 1 2 20 30 40 50
