In Python, how to drop a column in a Pandas Dataframe?
In Python, there are several ways to drop a column in a Pandas Dataframe. Let consider the following dataframe as an example:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = ["col1", "col2", "col3"])
Option 1: use pandas.dataframe.drop
.
df = df.drop("col2", axis=1)
Option 2: use pandas.dataframe.drop
without having to reassign df
thanks to the inplace=True
option.
df.drop("col2", axis=1, inplace=True)
Option 3: use del
.
del df["col2"]
In Python, there are several ways to drop a column in a Pandas Dataframe. Let consider the following dataframe as an example:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = ["col1", "col2", "col3"])
Option 1: use pandas.dataframe.drop
.
df = df.drop("col2", axis=1)
Option 2: use pandas.dataframe.drop
without having to reassign df
thanks to the inplace=True
option.
df.drop("col2", axis=1, inplace=True)
Option 3: use del
.
del df["col2"]
In Python, there are several ways to drop a column in a Pandas Dataframe. Let consider the following dataframe as an example:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = ["col1", "col2", "col3"])
Option 1: use pandas.dataframe.drop
.
df = df.drop("col2", axis=1)
Option 2: use pandas.dataframe.drop
without having to reassign df
thanks to the inplace=True
option.
df.drop("col2", axis=1, inplace=True)
Option 3: use del
.
del df["col2"]
In Python, there are several ways to drop a column in a Pandas Dataframe. Let consider the following dataframe as an example:
import pandas as pd
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns = ["col1", "col2", "col3"])
Option 1: use pandas.dataframe.drop
.
df = df.drop("col2", axis=1)
Option 2: use pandas.dataframe.drop
without having to reassign df
thanks to the inplace=True
option.
df.drop("col2", axis=1, inplace=True)
Option 3: use del
.
del df["col2"]
# | ID | Query | URL | Count |
---|---|---|---|---|
0 | 12108 | en | https://en.ans.wiki/334/in-python-how-to-drop-a-column-in-a-pandas-dataframe | 8 |