Chrome Extension

Pandas - Delete,Remove,Drop, column from pandas DataFrame

# Opiton one
del df['column_name']

# The best way to do this in pandas is to use drop:
df = df.drop('column_name', 1)

# where 1 is the axis number (0 for rows and 1 for columns.) To delete the column without having to reassign df you can do:
df.drop('column_name', axis=1, inplace=True)

# Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:
df = df.drop(df.columns[[0, 1, 3]], axis=1)  # df.columns is zero-based pd.Index this is called drop by index

# Another way is:
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
Code faster with devsheet chrome extension and increase productivity as a programmer
search code from your favorite search engine instantly.
Install Now
Was this helpful?
Open Code Manager We are hiring