Dropping columns by index in Pandas DataFrame
DataFrames can be very large and can contain hundreds of rows and columns. It is necessary to master the basic maintenance operations of DataFrames, such as deleting multiple columns. We can use dataframe.drop()
the method to delete columns or rows from a DataFrame based on the specified axis
, 0 for rows and 1 for columns. It determines the element to be deleted based on some labels. For example, we will delete columns from the following DataFrame 'a'
.
import pandas as pd
df = pd.DataFrame(
[[10, 6, 7, 8], [1, 9, 12, 14], [5, 8, 10, 6]], columns=["a", "b", "c", "d"]
)
print(df)
df.drop(["a"], axis=1, inplace=True)
print(df)
Output:
a b c d
0 10 6 7 8
1 1 9 12 14
2 5 8 10 6
b c d
0 6 7 8
1 9 12 14
2 8 10 6
Note inplace
the use of the parameter in the drop function. When inplace
the parameter is set to True
, the columns will be dropped from the original DataFrame; otherwise, a copy of the original DataFrame will be returned.
In our case, we have already deleted the columns 'a'
, but we need to pass their label names to dataframe.drop()
the function. When working with large datasets, we should handle such tasks for many columns at once and use column indices instead of their names.
We can dataframe.columns()
do this by using the method, which returns all the columns of the DataFrame, and passing the required column labels to the function using their indices dataframe.drop()
. The following code snippet explains how we can do this.
import pandas as pd
df = pd.DataFrame(
[[10, 6, 7, 8], [1, 9, 12, 14], [5, 8, 10, 6]], columns=["a", "b", "c", "d"]
)
df.drop(df.columns[[1, 2]], axis=1, inplace=True)
print(df)
Output:
a d
0 10 8
1 1 14
2 5 6
It will drop columns indexed by 1
or .2
We can also avoid using axis
the argument and just dataframe.drop()
mention columns
the parameter in the function and it will automatically indicate the columns to be deleted.
import pandas as pd
df = pd.DataFrame(
[[10, 6, 7, 8], [1, 9, 12, 14], [5, 8, 10, 6]], columns=["a", "b", "c", "d"]
)
df.drop(columns=df.columns[[1, 2]], inplace=True)
print(df)
Output:
a d
0 10 8
1 1 14
2 5 6
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Pandas Copy DataFrame
Publish Date:2025/05/01 Views:53 Category:Python
-
This tutorial will show you how to DataFrame.copy() copy a DataFrame object using the copy method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 ], "Cost" : [ "300" , "400" , "350" ], } ) print (items_df) Output:
Pandas DataFrame.ix[] Function
Publish Date:2025/05/01 Views:168 Category:Python
-
Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice
Pandas DataFrame.describe() Function
Publish Date:2025/05/01 Views:120 Category:Python
-
Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc
Pandas DataFrame.astype() Function
Publish Date:2025/05/01 Views:160 Category:Python
-
Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a
Pandas DataFrame.to_dict() Function
Publish Date:2025/05/01 Views:188 Category:Python
-
Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary. pandas.DataFrame.to_dict() Syntax DataFrame . to_dict(orient = 'dict' , into = class ' dict ' ) parameter orient This parameter determines the type of
Pandas DataFrame.reset_index() Function
Publish Date:2025/05/01 Views:140 Category:Python
-
Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels. pandas.DataFrame.rep
Pandas DataFrame.resample() Function
Publish Date:2025/05/01 Views:78 Category:Python
-
Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No
Pandas DataFrame.insert() Function
Publish Date:2025/05/01 Views:116 Category:Python
-
Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param
Pandas DataFrame.idxmax() Function
Publish Date:2025/05/01 Views:79 Category:Python
-
Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.