Nikillo
Nikillo

Reputation: 11

How do I remove a column from a dataframe using Pandas library for python?

Trying to remove a column from a dataframe with simple line of code using panda for python.

The name of the column that i'm trying to remove is "Comments"

import pandas as pd  
location2 = 'datasets_travel_times.csv'     
travelTime_df= pd.read_csv(location2)  
traveltime_df = travelTime_df.drop('Comments',1)  
traveltime_df

It does not give any error; but then I print the dataframe and see that the column "Comments" is still there

Upvotes: 1

Views: 160

Answers (1)

Ashish Agarwal
Ashish Agarwal

Reputation: 6283

Here are 2 possible ways:-

First Way:-

 travelTime_df.drop(['Comments'], axis=1)

Second way:-

travelTime_df.drop(columns=['Comments'])

Adding link for deep dive:- https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

Upvotes: 1

Related Questions