Deleting Columns in Pandas DataFrames?

Hello everyone! This is in regards to Exercise 6 in “How to Clean Data with Python” in the “Data Science Foundations” track, specifically the last task in that has you remove the old column with the untidy data.

The module has you do this by redefining the DataFrame with every column except the one you want to get rid of. This is well and good for this exercise, but what if you have a DataFrame with, say, a dozen or more columns and don’t want to type out each column in the interest of time? Is there a way to specifically target that one column you want gone, say with an operator statement? I tried something to the effect of this…

df = df[df.columns != df.column_to_delete]

…and it didn’t work. :stuck_out_tongue: Basically I couldn’t figure out the syntax, if it exists, and I’m asking for help here. Any help you all could give would be greatly appreciated.

Thank you!

You could do it a number of ways.

df.drop(['col1', 'col2'], axis=1)
or,

del df['column_name']

I prefer the 1st way. It’s always good to check the documentation:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.drop.html

1 Like

Thank you for that! :smiley:

1 Like