Remove Columns in Python Pandas
Intro
Now that you can add columns, let’s look at how to delete columns with Python Pandas.
Learn how to create and add new columns to a Pandas DataFrame using Python. In this tutorial, we will cover various methods to manipulate column data and explore the different ways to add columns to a DataFrame
Dropping Columns
You can use the drop
method to delete one or more columns from a DataFrame. The drop method takes a list of column labels to be deleted as its first argument, and the axis parameter, which should be set to 1, to specify that columns are being deleted. The inplace parameter, if set to True, modifies the original DataFrame and does not return a new one.
Assigning to a new dataframe
This is how you can assign a new dataframe, note that we are reassigning to the df
variable, which means we could also just do the deletion with inplace=True
and achieve the same thing
df = df.drop(["literalOne", "brand"], axis=1)
With inplace=True
df.drop(["literalOne", "brand"], axis=1, inplace=True)
Instead of deleting or removing columns, perhaps you can just select a subset of columns. Learn how in our next post.
Learn how to select specific columns with Python Pandas.
Related Posts
-
The Ultimate Python Pandas Guide
By: Adam RichardsonIn this ultimate guide, you will learn how to use Pandas to perform various data manipulation tasks, such as cleaning, filtering, sorting and aggregating data.
-
A Step-by-Step Guide to Joining Pandas DataFrames
By: Adam RichardsonLearn how to join pandas DataFrames efficiently with this step-by-step guide. Improve your data analysis skills and optimize your workflow today!
-
Appending DataFrames in Pandas: A Tutorial
By: Adam RichardsonLearn how to combine two DataFrames in Pandas using the Append function. This tutorial will guide you on how to join multiple DataFrames with code examples.
-
Calculating Mean Value Using mean() Function in Pandas
By: Adam RichardsonLearn how to use the mean() function in pandas to calculate the mean value of a dataset in Python. Improve your data analysis skills with this tutorial.