In this article, we will discuss how to drop columns in Pandas Dataframe by label Names or by Index Positions. Drop columns from a DataFrame can be achieved in multiple ways.
Let’s create a simple dataframe with a dictionary of lists, say column names are: ‘Name’, ‘Age’, ‘Place’, ‘College’.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# show the dataframe details |
Output :
Method 1: Drop Columns from a Dataframe using dataframe.drop()
method.
Example 1: Remove specific single mention column.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# Remove column name 'Age' rslt_df = details.drop([ 'Age' ],
axis = 1 )
# show the dataframe rslt_df |
Output :
Example 2 : Remove specific multiple mentions columns.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# Remove two columns name is 'Age' and # 'College' rslt_df = details.drop([ 'Age' , 'College' ],
axis = 1 )
# show the dataframe rslt_df |
Output :
Example 3: Remove columns as based on column index.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# Remove three columns as index base # 0, 1, 2 rslt_df = details.drop(details.columns[[ 0 , 1 , 2 ]],
axis = 1 )
# show the dataframe rslt_df |
Output :
Method 2: Drop Columns from a Dataframe using iloc[]
and drop()
method.
Example: Remove all columns between a specific column to another columns(exclude)
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# Remove all columns from column # index 1 to 3(exclude) rslt_df = details.drop(details.iloc[:, 1 : 3 ],
axis = 1 )
# show the dataframe rslt_df |
Output :
Method 3: Drop Columns from a Dataframe using loc[]
and drop()
method.
Example: Remove all columns between a specific column name to another columns name.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# Remove all columns from column name # 'Name' to 'College' rslt_df = details.drop(details.loc[:, 'Name' : 'College' ].columns,
axis = 1 )
# show the dataframe # only indexes print rslt_df |
Output :
Note: Different loc()
and iloc()
is iloc() exclude last column range element.
Method 4: Drop Columns from a Dataframe by iterative way.
Example: Remove specific column.
# import pandas library as pd import pandas as pd
# List of Tuples students = [( 'Ankit' , 22 , 'Up' , 'Geu' ),
( 'Ankita' , 31 , 'Delhi' , 'Gehu' ),
( 'Rahul' , 16 , 'Tokyo' , 'Abes' ),
( 'Simran' , 41 , 'Delhi' , 'Gehu' ),
( 'Shaurya' , 33 , 'Delhi' , 'Geu' ),
( 'Harshita' , 35 , 'Mumbai' , 'Bhu' ),
( 'Swapnil' , 35 , 'Mp' , 'Geu' ),
( 'Priya' , 35 , 'Uk' , 'Geu' ),
( 'Jeet' , 35 , 'Guj' , 'Gehu' ),
( 'Ananya' , 35 , 'Up' , 'Bhu' )
]
# Create a DataFrame object from # list of tuples with columns # and indices. details = pd.DataFrame(students,
columns = [ 'Name' , 'Age' ,
'Place' , 'College' ],
index = [ 'a' , 'b' , 'c' , 'd' , 'e' ,
'f' , 'g' , 'i' , 'j' , 'k' ])
# loop throughout all the columns for column in details.columns :
if column = = 'Age' :
# delete the column
del details[column]
# show the dataframe details |
Output :