Merge the first row with the column headers in a dataframe

前端 未结 3 335
野性不改
野性不改 2021-01-13 01:56

I am trying to clean up a Excel file for some further research. Problem that I have, I want to merge the first and second row. The code which I have now:

xl         


        
3条回答
  •  北海茫月
    2021-01-13 02:40

    Fetch the all columns present in Second row header then First row header. combine them to make a "all columns name header" list. now create a df with excel by taking header as header[0,1]. now replace its headers with all column name headers you created previously.

    import pandas as pd
    
    #reading Second header row columns
    df1 = pd.read_excel('nanonose.xls', header=[1] , index = False)
    cols1 = df1.columns.tolist()
    SecondRowColumns = []
    for c in cols1:
        if ("Unnamed" or "NaN" not in c):
            SecondRowColumns.append(c)     
    
    #reading First header row columns
    df2 = pd.read_excel('nanonose.xls', header=[0] , index = False)
    cols2 = df2.columns.tolist()
    FirstRowColumns = []
    for c in cols2:
        if ("Unnamed" or "Nanonose" not in c):
            FirstRowColumns.append(c)       
    
    AllColumn = []
    AllColumn = SecondRowColumns+ FirstRowColumns
    
    
    
    df = pd.read_excel('nanonose.xls', header=[0,1] , index=False)
    df.columns = AllColumn
    print(df)
    

提交回复
热议问题