How To Select All Columns Except One Column in Pandas

Do you want to select all the columns except one while using Pandas in Python? This article will discuss how to select all columns except one column in Pandas in Python.

In Pandas you usually use the columns method to select all the columns in the data represented in rows and columns. And using this method only you can easily select the specific columns that you want from the data table.

How To Select All Columns Except One using Pandas

As discussed above you can use the columns method present inside the Pandas library of Python. This method allows you the features to select specific and unselect specific columns also we need to use the loc method to specify which column or row we do now want to select.

Let us see the usage of the columns and loc method in the below code example.

#importing Pandas
import pandas as pd

#Initializing the Data
data = {'Column1':[1,2,3,4,5], 'Column2':[6,7,8,9,10], 'Column3':[11,12,13,14,15]}

#Initializing the DataFrame
df = pd.DataFrame(data, columns = ['Column1', 'Column2', 'Column3'])

#Selecting Column1 and Column3 and excluding Column2
selectingColumns = df.loc[:, df.columns != 'Column2']

#Printing the Columns Data except Column2
print(selectingColumns)

Output:

   Column1  Column3
0        1       11
1        2       12
2        3       13
3        4       14
4        5       15

As you can see in the above code, using the loc function I was able to tell which columns need to be excluded from being selected and then store the remaining columns data in the variable. And printing the columns data successfully.

Note that this method operates in-place and your dataframe is modified using this method. If you do not want to make an inplace modification then you can use the below method.

Using drop Method to Select All Columns Except on In Pandas

Pandas library also has a method named drop[1]. This method does not operate inplace for your dataframe and hence your dataframe is secure even after dropping the column from being selected.

But the drop function provides you the option to mark the inplace option as true if you want to modify the dataframe as well.

Let us see in the below example code the usage of the drop method in Pandas in Python.

#importing Pandas
import pandas as pd

#Initializing the Data
data = {'Column1':[1,2,3,4,5], 'Column2':[6,7,8,9,10], 'Column3':[11,12,13,14,15]}

#Initializing the DataFrame
df = pd.DataFrame(data, columns = ['Column1', 'Column2', 'Column3'])

#Selecting Column1 and Column2 and excluding Column3 using Drop Method
selectingColumns = df.drop('Column3', axis=1)

#Printing the Columns Data except Column3
print(selectingColumns)

Output:

   Column1  Column2
0        1        6
1        2        7
2        3        8
3        4        9
4        5       10

As you can see in the above code, I was able to omit the column3 data from the dataframe using the drop method. One more advantage of the drop method is that you can drop as many columns as you want.

Let us see in the below example dropping multiple columns using the drop method in Pandas in Python.

Drop Multiple columns from being selected

#importing Pandas
import pandas as pd

#Initializing the Data
data = {'Column1':[1,2,3,4,5], 'Column2':[6,7,8,9,10], 'Column3':[11,12,13,14,15]}

#Initializing the DataFrame
df = pd.DataFrame(data, columns = ['Column1', 'Column2', 'Column3'])

#Selecting Column1 excluding Column3,Column2 using drop Method
selectingColumns = df.drop(['Column3', 'Column2'], axis=1)

#Printing the Columns Data except Column3,Column2
print(selectingColumns)

Output

   Column1
0        1
1        2
2        3
3        4
4        5

As you can see the drop method accepts the list of column names that can be used to omit the number of columns at once if you do not want to select them.

How To Select All Columns Except One Column in Pandas

Wrap Up

I hope you were able to get the answer related to how to select all the columns except one in pandas. I have listed two methods that you can use and each one of them has its own advantages and you can use them as per your convenience.

If you think you know a better method than the one discussed above then please let me know in the comment section I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Iterate Over Rows in a DataFrame in Pandas Python
  2. Python Get Filename From A Path Without Extension 4 Ways
  3. How To Concatenate String and Integer In Python
  4. Code Example: Convert a String to DateTime in Python
  5. How Do I Sort A Dictionary By Value In Python
  6. Code Example: How To Add Numbers In A List Python
  7. Python – How To Delete All Files In A Directory

Leave a Comment