Prompt engineering sucks. Break free from the endless tweaking with this revolutionary approach  - Learn more

Securing AI systems is tricky, ignoring it is risky. Discover the easiest way to secure your AI end to end  - Learn more

Back to Blog
How-To

How to Select All Columns Except One in a DataFrame

Aporia Team Aporia Team 2 min read Sep 06, 2022

Consider a DataFrame with a lot of columns and we need all of them except for one. In this short how-to article, we will learn a convenient way of selecting all columns except for one.

Select All Columns Except in Dataframe - Pandas and Pyspark

Pandas

The loc method is used for selecting rows and columns by their labels. We will write a condition in the loc method using the columns method and the name of the unwanted column.

df.loc[:, df.columns != "f3"]

PySpark

We can use a list comprehension in the select function to create a list of the desired columns.

df.select([col for col in df.columns if col != "f2"])

The expression inside the select function is a list comprehension that creates a list with all the column names if it is not “f2”.

It is important to note that another way of solving this task is to drop the undesired column. Both Pandas and PySpark have a drop function to do this operation.

This question is also being asked as:

  • Retrieve DataFrame of all but one specified column.

People have also asked for:

Rate this article

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

On this page

Blog
Building a RAG app?

Consider AI Guardrails to get to production faster

Learn more
Table of Contents

Related Articles