In this blog post, we will discuss how to delete a column from a Pandas DataFrame. This is a very simple process, but it can be confusing for beginners.
We will walk you through the steps needed to delete a column, and explain what each step does.
Let’s get started!
Delete a column from a Pandas DataFrame
The first step is to import the Pandas library. We do this with the following code:
import pandas as pd
- Next, we will create a DataFrame with some sample data. We can do this with the following code:]
df = pd.DataFrame({'A': [0, 0, 0], 'B': [0, 0, 0], 'C': [0, 0, 0]})
- Now that we have our DataFrame created, we can delete a column.
To delete the ‘A’ column, we use the following code:
del df['A']
- This code will delete the ‘A’ column from our DataFrame.
You can verify this by printing the DataFrame:
print(df)
This should give you the following output:
Get full code:
import pandas as pd df = pd.DataFrame({'A': [0, 0, 0], 'B': [0, 0, 0], 'C': [0, 0, 0]}) del df['A'] print(df)
Output
B C
0 0 0
1 0 0
2 0 0
As you can see, the ‘A’ column has been successfully deleted.
Conclusion
That’s all there is to delete a column from a Pandas DataFrame! We hope this blog post has been helpful. If you have any questions, please leave a comment below. Thank you for reading!
Read More: How do I Get the Row Count of a Pandas Data Frame