Introduction
In this tutorial, we want to drop columns from a Pandas DataFrame. In order to do this, we use the the drop() method of Pandas.
Import Libraries
First, we import the following python modules:
import pandas as pd
Create Pandas DataFrame
Next, we create a Pandas DataFrame with some example data from a dictionary:
mydict = {
"language": ["Python", "Python", "Java", "JavaScript"],
"framework": ["Django", "FastAPI", "Spring", "ReactJS"],
"users": [20000, 9000, 7000, 5000],
}
df = pd.DataFrame(mydict)
df
Drop a Single Column
We would like to remove a single column from the DataFrame.
To do this, we use the drop() method of Pandas and pass the column name as argument:
new_df = df.drop(columns='users')
new_df
Drop Multiple Columns
Next, we would like to remove multiple columns from the DataFrame.
To do this, we use the drop() method of Pandas and pass the column names as arguments:
new_df = df.drop(columns=['framework', 'users'])
new_df
Conclusion
Congratulations! Now you are one step closer to become an AI Expert. You have seen that it is very easy to drop columns from a Pandas DataFrame. We can simply use the drop() method of Pandas. Try it yourself!
Also check out our Instagram page. We appreciate your like or comment. Feel free to share this post with your friends.