Pandas - delete column name

后端 未结 3 746
故里飘歌
故里飘歌 2020-12-06 16:30

I want to delete to just column name (x,y,z) use only data

In [68]: df
Out[68]: 
   x  y  z
0  1  0  1  
1  2  0  0 
2  2  1  1 
3  2  0  1 
4  2  1  0


        
相关标签:
3条回答
  • 2020-12-06 16:54

    If all you need is to print out without the headers then you can use the to_string() and set header=False, e.g.:

    >>> print(df.to_string(header=False))
    0  1  0  1
    1  2  0  0
    2  2  1  1
    3  2  0  1
    4  2  1  0
    
    0 讨论(0)
  • 2020-12-06 17:12

    If you need to remove the header alone, uses '.values'.

    df = df[:].values
    

    But the above code will return a numpy array instead of dataframe. Converting the same again into dataframe will add default values to column names (0,1..).

    0 讨论(0)
  • 2020-12-06 17:14

    In pandas by default need column names.

    But if really want 'remove' columns what is strongly not recommended, because get duplicated column names is possible assign empty strings:

    df.columns = [''] * len(df.columns)
    

    But if need write df to file without columns and index add parameter header=False and index=False to to_csv or to_excel.

    df.to_csv('file.csv', header=False, index=False)
    
    df.to_excel('file.xlsx', header=False, index=False)
    
    0 讨论(0)
提交回复
热议问题