27

I want to delete to just column name (x,y,z), and 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

I want to print result to same as below.

Out[68]: 

0  1  0  1  
1  2  0  0 
2  2  1  1 
3  2  0  1 
4  2  1  0

Is it possible? How can I do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
lil
  • 439
  • 3
  • 8
  • 17

5 Answers5

42

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)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • wow! header=false.. so i appreciate it! i want to know about it !! – lil Jul 05 '17 at 05:38
  • So you need remove column names because need write to file without them? – jezrael Jul 05 '17 at 05:40
  • right .. i want to write data without columnname and index. – lil Jul 05 '17 at 05:44
  • @jezrael how to keep the 1st dataframe columns names and remove the next dataframe column names in to_excel? – dondapati Mar 13 '19 at 07:49
  • @dondapati - Not sure if understand, can you explain more? – jezrael Mar 13 '19 at 07:50
  • @ jezrael ,using the for loop i created multiple dataframes ,here column names are same for all dataframes .now i want keep 1st dataframe column as reference for all dataframes. and export in to excel. – dondapati Mar 13 '19 at 07:52
  • @dondapati - hmmm, so I suggest use `for i, df in enumerate(dfs):` and if `i == 1` then save columns names, if `i > 0` not. – jezrael Mar 13 '19 at 07:56
12

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
AChampion
  • 29,683
  • 4
  • 59
  • 75
9

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..).

Sajin Sabu
  • 109
  • 1
  • 2
  • this work perectly!! It is a shame that a command like df.rename(columns=None) or similar doesn't exist. – SimAzz Nov 21 '22 at 16:13
3

First find the number of columns by:

df.shape  # it helps you to know the total no of columns you have (as well as rows)

Lets say shape is (188,8):

df.columns = np.arange(8)  #here we have `8` columns

This makes the columns as int64 starting from 0 to 7 .

Mario
  • 1,631
  • 2
  • 21
  • 51
Rohan
  • 31
  • 3
-1

You can use this:

df.name = None
Dharman
  • 30,962
  • 25
  • 85
  • 135
江祐宏
  • 7
  • 2
  • 1
    This does not delete the column but assigns `None` all rows in column `name`. – yoonghm Nov 13 '21 at 01:05
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – PCM Nov 13 '21 at 05:29
  • This is wrong, please delete it – smci Jul 18 '22 at 22:27