In this article, you’ll learn how to remove a column in Pandas. I will give three different examples that will help you to understand how to remove columns.
- Remove a single column in Pandas
- Remove multiple columns in Pandas
- Remove columns from the dataset
Example 1: Remove a single column in Pandas
# Import the Pandas library as pd import pandas as pd # Initialize a data in the form of dictionary data = {'Names':['John', 'Harry', 'Ahmad', 'Ali'], 'CGPA':[3.5, 4.0, 3.0, 4.0], 'Age':[25, 31, 23, 28]} # Convert data into Pandas DataFrame df = pd.DataFrame(data) # Display the Original DataFrame print(df) # Remove a Single Column df1 = df.drop(['CGPA'], axis=1) # Display a new DataFrame df1 print(df1)
Output:
Names CGPA Age 0 John 3.5 25 1 Harry 4.0 31 2 Ahmad 3.0 23 3 Ali 4.0 28 Names Age 0 John 25 1 Harry 31 2 Ahmad 23 3 Ali 28
Example 2: Remove multiple columns in Pandas
# Import the Pandas library as pd import pandas as pd # Initialize a data in the form of dictionary data = {'Names':['John', 'Harry', 'Ahmad', 'Ali'], 'CGPA':[3.5, 4.0, 3.0, 4.0], 'Age':[25, 31, 23, 28]} # Convert data into Pandas DataFrame df = pd.DataFrame(data) # Display the Original DataFrame print(df) # Remove Multiple Columns and save results in the same variable using inplace df.drop(['CGPA','Age'], axis=1, inplace = True) # Display DataFrame df after modification print(df)
Output:
Names CGPA Age 0 John 3.5 25 1 Harry 4.0 31 2 Ahmad 3.0 23 3 Ali 4.0 28 Names 0 John 1 Harry 2 Ahmad 3 Ali
Example 3: Remove columns from the dataset
# Import the Pandas library as pd import pandas as pd # Read the CSV file as Pandas DataFrame df = pd.read_csv("weather.csv") # Display the Original DataFrame print(df) # Drop a single column and changes applied to original dataframe using inplace df.drop(['min_temp'], axis = 1, inplace = True) # Display the DataFrame after modification print(df)
Output:
date min_temp max_temp 0 01/01/2013 29.8 15.2 1 02/01/2013 32.1 16.8 2 03/01/2013 34.3 18.5 3 04/01/2013 33.1 17.6 4 05/01/2013 31.5 14.6 .. ... ... ... 408 13/02/2014 31.5 24.3 409 14/02/2014 31.2 24.5 410 15/02/2014 31.7 25.0 411 16/02/2014 33.5 24.4 412 17/02/2014 29.1 25.0 [413 rows x 3 columns] date max_temp 0 01/01/2013 15.2 1 02/01/2013 16.8 2 03/01/2013 18.5 3 04/01/2013 17.6 4 05/01/2013 14.6 .. ... ... 408 13/02/2014 24.3 409 14/02/2014 24.5 410 15/02/2014 25.0 411 16/02/2014 24.4 412 17/02/2014 25.0 [413 rows x 2 columns]
If you want to learn Pandas, I will recommend this book and free resource.