How to drop one or multiple columns from Dataframe
The Pandas DataFrame is a versatile data structure that can handle two-dimensional labeled data with columns of different data types. The drop() method allows you to remove rows or columns by specifying the label names and the corresponding axis (0 for rows and 1 for columns) that you want to drop, or you can directly specify the index or column names to be removed.
Drop column "Promoted"
where 1 is the axis number (0 for rows and 1 for columns.)
In order to delete the column without having to reassign DataFrame you can do:
Please note that "inplace=True" was added in pandas v0.13 and it won't work on older versions.
Using Column Number
To drop columns by column number in a Pandas DataFrame, you can use the iloc method. For example, if you want to drop the columns at index 0 and 1, you can use the following syntax:
This will remove the columns at index 0 and 1 from the DataFrame df.
The errors='ignore' parameter in the drop() method of Pandas DataFrame is available from version 0.16.1 onwards. When using errors='ignore', it allows you to drop columns only if they exist in the DataFrame. If the specified column labels are not found in the DataFrame, it will not raise an error and will simply ignore those labels while dropping columns. This can be useful to avoid errors when trying to drop columns that may or may not be present in the DataFrame.
When you run this code, you will get the message KeyError: "['NotExist'] not found in axis" . If you use the argument errors='ignore' then drop() method will only drop the existing columns from the labels passed to it.
To drop one or multiple columns from a Pandas DataFrame, you can use the drop() method with the columns parameter. You can specify the column labels or column numbers that you want to drop, and use the inplace=True argument to modify the original DataFrame. Additionally, you can use errors='ignore' to drop only the existing columns and avoid errors for non-existing columns.