Pandas Dataframe add header without replacing current header

后端 未结 2 1889
予麋鹿
予麋鹿 2020-12-29 13:26

How can I add a header to a DF without replacing the current one? In other words I just want to shift the current header down and just add it to the dataframe as another rec

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 14:02

    The key is to specify header=None and use column to add header:

    data = pd.read_csv('file.csv', skiprows=2, header=None ) # skip blank rows if applicable
    df = pd.DataFrame(data)
    df = df.iloc[ : , [0,1]] # columns 1 and 2
    df.columns = ['A','B'] # title
    

提交回复
热议问题