Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove/omit the count column from the dataframe in Pandas?

Tags:

python

pandas

This is probably very simple, but I've searched for hours and I can't find how to remove/omit column 1 (count column) in pandas?

Currently looks like:

    ID     Test1    Test2   Test3
1   236    data1    data2   data3   
2   323    data4    data5   data3
3   442    data6    data2   data4
4   543    data8    data2   data3
5   676    data1    data8   data4

Needs to look like:

ID     Test1    Test2   Test3
236    data1    data2   data3   
323    data4    data5   data3
442    data6    data2   data4
543    data8    data2   data3
676    data1    data8   data4

Code Snippet:

df = df[['ID','Test1','Test2','Test3']]
df.sort_values(['ID'], ascending=[True], inplace=True)
return render_template('index.html', data=df.to_html(index_names=False))

Thanks!

like image 518
Mike Avatar asked Apr 08 '16 12:04

Mike


People also ask

How do you omit a column in pandas?

We can exclude one column from the pandas dataframe by using the loc function. This function removes the column based on the location.

How do I exclude the last column in a DataFrame?

You can also use DataFrame. drop() method to delete the last n columns. Use axis=1 to specify the columns and inplace=True to apply the change on the existing DataFrame.

How do you omit columns in python?

You can use the following syntax to exclude columns in a pandas DataFrame: #exclude column1 df. loc[:, df. columns!='


1 Answers

If you want to use a specific column as your index then you just use set_index:

In [24]:
df.set_index('ID', inplace=True)
df

Out[24]:
     Test1  Test2  Test3
ID                      
236  data1  data2  data3
323  data4  data5  data3
442  data6  data2  data4
543  data8  data2  data3
676  data1  data8  data4
like image 189
EdChum Avatar answered Sep 18 '22 15:09

EdChum