{"id":8116,"date":"2021-02-17T22:21:12","date_gmt":"2021-02-17T16:51:12","guid":{"rendered":"https:\/\/pynative.com\/?p=8116"},"modified":"2023-03-09T11:52:55","modified_gmt":"2023-03-09T06:22:55","slug":"pandas-drop-columns","status":"publish","type":"post","link":"https:\/\/pynative.com\/pandas-drop-columns\/","title":{"rendered":"Drop columns in pandas DataFrame"},"content":{"rendered":"\n
Datasets could be in any shape and form. To optimize the data analysis, we need to remove some data that is redundant or not required. This article aims to discuss all the cases of dropping single or multiple columns from a pandas DataFrame<\/a>.<\/p>\n\n\n\n The following functions are discussed in this article in detail:<\/p>\n\n\n\n In the last section, we have shown the comparison of these functions. So stay tuned…<\/p>\n\n\n\n Also, See:<\/strong><\/p>\n\n\n\n We can use this pandas function to remove the columns or rows from simple as well as multi-index DataFrame.<\/p>\n\n\n Parameters:<\/strong><\/p>\n\n\n\n Returns:<\/strong><\/p>\n\n\n\n We may need to delete a single or specific column from a DataFrame.<\/p>\n\n\n\n In the below example we drop the ‘age<\/strong>‘ column from the DataFrame using \n
df.drop(columns = ['col1','col2'...])<\/code><\/li>\n\n\n\n
df.pop('col_name')<\/code><\/li>\n\n\n\n
del df['col_name']<\/code><\/li>\n<\/ul>\n\n\n\n
\n
Table of contents<\/h2>
The
DataFrame.drop()<\/code> function<\/h2>\n\n\n\n
DataFrame.drop(labels=None<\/span>, axis=1<\/span>, columns=None<\/span>, level=None<\/span>, inplace=False<\/span>, errors='raise'<\/span>)<\/code><\/span>Code language:<\/span> Python<\/span> (<\/span>python<\/span>)<\/span><\/small><\/pre>\n\n\n
\n
labels<\/code><\/strong>: It takes a list of column labels to drop.<\/li>\n\n\n\n
axis<\/code><\/strong>: It specifies to drop columns or rows. set a
axis<\/code> to 1<\/strong> or \u2018columns<\/strong>\u2019 to drop columns. By default, it drops the rows from DataFrame.<\/li>\n\n\n\n
columns<\/code><\/strong>: It is an alternative to
axis='columns'<\/code>. It takes a single column label or list of column labels as input.<\/li>\n\n\n\n
level<\/code><\/strong>: It is used in the case of a MultiIndex DataFrame to specify the level from which the labels should be removed. It takes a level position or level name as input.<\/li>\n\n\n\n
inplace<\/code><\/strong>: It is used to specify whether to return a new DataFrame or update an existing one. It is a boolean flag with default False<\/strong>.<\/li>\n\n\n\n
errors<\/code><\/strong>: It is used to suppress
KeyError<\/code><\/strong> error if a column is not present. It takes the following inputs:
‘ignore<\/strong>‘: It suppresses the error and drops only existing labels.
‘raise<\/strong>‘: Throws the errors if the column does not exist. It is the default case.<\/li>\n<\/ol>\n\n\n\n\n
inplace=True<\/code><\/li>\n\n\n\n
KeyError<\/code> if labels are not found.<\/li>\n<\/ul>\n\n\n\n
Drop single column<\/h2>\n\n\n\n
df.drop(columns = 'col_name')<\/code><\/p>\n\n\n