Skip to content

Instantly share code, notes, and snippets.

@reservoirinvest
Last active November 9, 2017 02:59
Show Gist options
  • Save reservoirinvest/bdd911243df3f24b874a45a1fa01830f to your computer and use it in GitHub Desktop.
Save reservoirinvest/bdd911243df3f24b874a45a1fa01830f to your computer and use it in GitHub Desktop.
Drop columns in pandas #pandas
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)
# To drop a list of columns
columns = ['Col1', 'Col2', ...]
df.drop(columns, inplace=True, axis=1)
Finally, to drop by column number instead of by column label, try this to delete, e.g. the 1st, 2nd and 4th columns:
# df.drop(df.columns[[0, 1, 3]], axis=1) # df.columns is zero-based pd.Index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment