Pandas Drop Column
Method 1 – Pandas Drop()
Pandas provide the drop() method, which allows us to remove a column based on the column label and its corresponding axis.
The function syntax is as shown below:
The function parameters are as described below:
- Labels – specifies the index or the column’s label to delete. You can pass a single value or a list-like object.
- axis – specifies which axis the selected column label is to be deleted.
- index – alternative to the axis.
- columns – alternative to the axis.
- Inplace – specifies if the operation is to be performed in-place or not.
- errors – sets the action when errors arise. Accepted values are ‘raise’ and ‘ignore,’ which raises errors when they occur and suppresses errors.
Let us illustrate how we can use this function to drop a column.
Start by creating a simple pandas DataFrame as shown in the example below:
import pandas as pd
# create dataframe
df = pd.DataFrame({
'db_type': ['relational', 'key-value', 'document', 'search-engine'],
'default_port': [3306, 6379, 27017, 9200]
},
index=['MySQL', 'Redis', 'MongoDB', 'Elasticsearch'])
df
The code above should return a DataFrame as shown:
To delete the Redis entry from the DataFrame, we can run the code:
In the example above, we use the drop() function to delete the column ‘Redis’ from the DataFrame.
Note: We set the in-place parameter to false. This prevents the function from modifying the original DataFrame.
NOTE: Setting the axis=0 will remove a row instead of a column. If you wish to remove a column, set the axis parameter to one.
This should return:
Method 2 – Pop
You can also use the pop() method to remove a column from a DataFrame. An example is illustrated below:
In this case, the function will remove the ‘default_port’ column from the DataFrame.
Closing
This tutorial covers two primary methods you can use to remove a column and rows from a Pandas DataFrame. Feel free to explore the docs for more.
Source: linuxhint.com