How to remove the first column of a Pandas DataFrame?

In today’s tutorial we’ll learn how use Python in order to remove the first column of a Pandas DataFrame.

Let’s assume, that we have a DataFrame that has a couple of columns as well as a sequential index:

# import Pandas library

import pandas as pd


# define example data
interview_dict = {'language': [ 'Python', 'R', 'Scala', 'Java', 'SQL'],
                 'salary':[130, 110, 85, 95, 77]}

interviews = pd.DataFrame(data=interview_dict)

interviews.head()

Here’s the DataFrame that we have just created:

Removing the index column

If we want to get rid of the index column (the first leftmost column that allows to label each and every one of the DataFrame rows) we’ll execute the following steps:

  • First off, we will export and save our DataFrame as a Comma Separated Value (csv) file using the df.to_csv() method. We will ensure that we are not exporting the index along with the data columns by using the index=False parameter.
interviews.to_csv('interviews.csv', index=False)
  • Next, we’ll import the csv file contents, but explicitly indicate other column in our dataset as the index:
iv1= pd.read_csv('interviews.csv', index_col = 'language')
iv1.head()

Here’s the result:

Drop the first data column in a DataFrame

You might as well want to drop the first column of your data table. You can refer to it by its label (name) or column index.

cols = interviews.columns[0]
iv2 = interviews.drop(columns = cols)

Or alternatively by label:

cols = ['language']
iv1 = interviews.drop(columns= cols)

Both will render the same result:

Next Suggested Learning