Removing header column from pandas dataframe

前端 未结 4 601
眼角桃花
眼角桃花 2020-12-09 08:46

I have the foll. dataframe:

df

   A   B
0  23  12
1  21  44
2  98  21

How do I remove the column names A and B

4条回答
  •  有刺的猬
    2020-12-09 09:25

    I think you cant remove column names, only reset them by range with shape:

    print df.shape[1]
    2
    
    print range(df.shape[1])
    [0, 1]
    
    df.columns = range(df.shape[1])
    print df
        0   1
    0  23  12
    1  21  44
    2  98  21
    

    This is same as using to_csv and read_csv:

    print df.to_csv(header=None,index=False)
    23,12
    21,44
    98,21
    
    print pd.read_csv(io.StringIO(u""+df.to_csv(header=None,index=False)), header=None)
        0   1
    0  23  12
    1  21  44
    2  98  21
    

    Next solution with skiprows:

    print df.to_csv(index=False)
    A,B
    23,12
    21,44
    98,21
    
    print pd.read_csv(io.StringIO(u""+df.to_csv(index=False)), header=None, skiprows=1)
        0   1
    0  23  12
    1  21  44
    2  98  21
    

提交回复
热议问题