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.
errors='ignore'
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.
Conclusion
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.
- Creating an empty Pandas DataFrame
- How to Check if a Pandas DataFrame is Empty
- How to check if a column exists in Pandas Dataframe
- How to select multiple columns from Pandas DataFrame
- Selecting multiple columns in a Pandas dataframe based on condition
- Selecting rows in pandas DataFrame based on conditions
- How to Drop rows in DataFrame by conditions on column values
- Rename column in Pandas DataFrame
- Get a List of all Column Names in Pandas DataFrame
- How to add new columns to Pandas dataframe?
- Change the order of columns in Pandas dataframe
- Concatenate two columns into a single column in pandas dataframe
- How to count the number of rows and columns in a Pandas DataFrame
- Use a list of values to select rows from a pandas dataframe
- How to iterate over rows in a DataFrame in Pandas
- How to drop rows/columns of Pandas DataFrame whose value is NaN
- How to Export Pandas DataFrame to a CSV File
- Convert list of dictionaries to a pandas DataFrame
- How to set a particular cell value in pandas DataFrame