Pandas functions
Pandas Drop – pd.DataFrame.drop()
Drop rows and columns in pandas
If you’re looking for information on how to pandas drop a column and return it to you, check out pandas pop.
A lot of data can be too much data. This is when you need to remove items from your dataset. Pandas Drop is what you’re looking for
Drop – pd.DataFrame.Drop() removes data (rows or columns) from your DataFrame. It’s extremely useful when dropping a single or multiple rows or columns.
Pseudo code: Remove (or drop) single/multiple rows/columns from a DataFrame or Series
Example: Below we drop the column “Type” from our DataFrame
Pandas Drop
Let’s take a look at the different parameters you can pass pd.drop():
- Labels (required) – This is where you can pass a single column/row name, or multiple. Pass a single value, or else a list-like of values to drop multiple columns
- Axis (Default 0) – You can set axis to specify whether you want to drop rows, or columns. Axis = 0 or ‘index’ tells Pandas you want to remove rows. Lastly, axis = 1 or ‘columns tells Pandas you want to remove columns.
- Index – Optional field where you can specify a single value or a list of rows to drop. If you set the index parameter to a value, then Pandas assumes that you’re dropping rows. If set, there is no need to specify “Axis”
- Columns – Similar to “Index”, if the “columns” parameter is set, then Pandas assumes that you’re dropping columns. You do not need to set “Axis”
- Level – For when you have a MultiIndex. Here you can specify which level of the MultiIndex that your “Labels” (from above) refer to.
- Inplace (Default: False) – If set to ‘False’ then Pandas will drop the data, and return a copy of your DataFrame. If ‘True’, then pandas will drop the data and overwrite your existing DataFrame.
- Errors (Default: ‘Raise’) – Basically, do you want to see your errors raised or not? You’ll get an error if you reference a row or a column that doesn’t exist. If you want to ignore errors, then set errors to “ignore”