Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete column name

Tags:

python

pandas

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?

like image 297
lil Avatar asked Jul 05 '17 05:07

lil


People also ask

How do I delete a specific column in SQL?

First, write ALTER TABLE , followed by the name of the table you want to change (in our example, product ). Next add the DROP COLUMN clause, followed by the name of the column you want to remove (in our example, description ). This removes the column from the table, including any data stored in it.

How do I remove a column name in R?

In R, the easiest way to remove columns from a data frame based on their name is by using the %in% operator. This operator lets you specify the redundant column names and, in combination with the names() function, removes them from the data frame. Alternatively, you can use the subset() function or the dplyr package.


4 Answers

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)
like image 183
jezrael Avatar answered Sep 28 '22 12:09

jezrael


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
like image 33
AChampion Avatar answered Sep 28 '22 12:09

AChampion


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

like image 28
Sajin Sabu Avatar answered Sep 28 '22 12:09

Sajin Sabu


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 .

like image 45
Rohan Avatar answered Sep 28 '22 13:09

Rohan