How to drop all columns except one in Pandas

Here is a tutorial on how to drop all columns except one in Pandas.

Data scientists often work with huge tables of data. What to do when you only need data from one column and want to drop all other columns.

How to drop all columns except one

In Pandas you can very easily drop all columns except one with one function.

This is my sample data frame.

import pandas as pd

my_df = pd.DataFrame({'id':['id1','id2','id3'],
                          'Column1': ['2', '7', '6'],
                           'Column2': ['4', '5', '8'],
                           'Column3': ['4', '1', '9'],
                           'Column4': ['3', '3', '8'],
                      'Column5': ['2', '5', '4'],
                      'Column6': ['2', '7', '3']})

print(my_df)

To get rid of all other columns except one, use the loc function. The code I pasted below will remove all columns from the data frame except the one I entered.

import pandas as pd

my_df = pd.DataFrame({'id':['id1','id2','id3'],
                          'Column1': ['2', '7', '6'],
                           'Column2': ['4', '5', '8'],
                           'Column3': ['4', '1', '9'],
                           'Column4': ['3', '3', '8'],
                      'Column5': ['2', '5', '4'],
                      'Column6': ['2', '7', '3']})

my_df = my_df.loc[:, ['Column2']]

print(my_df)

how to drop all columns except one in Pandas

Pandas removed all columns except the one I entered.

How to drop all columns except one by index

You must use an alternative method if you do not know the column name. You then need to point to the column by index. In this case, the loc function will not work, and you must use the iloc function.

import pandas as pd

my_df = pd.DataFrame({'id':['id1','id2','id3'],
                          'Column1': ['2', '7', '6'],
                           'Column2': ['4', '5', '8'],
                           'Column3': ['4', '1', '9'],
                           'Column4': ['3', '3', '8'],
                      'Column5': ['2', '5', '4'],
                      'Column6': ['2', '7', '3']})

my_df = my_df.iloc[:, [2]]

print(my_df)

I told iloc to remove all columns except the third one. Note that indexes start at 0.

Now you know how to drop all but one column in Pandas.

This Post Has 3 Comments

Leave a Reply