Last active
November 9, 2017 02:59
-
-
Save reservoirinvest/bdd911243df3f24b874a45a1fa01830f to your computer and use it in GitHub Desktop.
Drop columns in pandas #pandas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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