JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Check if a column exists in Pandas

Author:JIYIK Last Updated:2025/04/12 Views:

This tutorial demonstrates methods to check if a column in a Pandas Dataframe exists in Python. We will use the INand NOT INoperators that can be used to perform this operation in Python.


Check if a column exists in Pandas using INthe operator

Dataframe is an arrangement that holds two-dimensional data and its corresponding labels. We can dataframe.columnfind the column labels using the column attribute.

To ensure that the column exists, we use INthe expression. However, before we begin, we need to form a dummy DataFrame in Pandas to use the above technique.

Here, we have created a DataFrame of student performance with column names Name, , Promotedand Marks.

import pandas as pd
import numpy as np

# Creating dataframe
df = pd.DataFrame()
# Adding columns to the dataframe
df["Name"] = ["John", "Doe", "Bill"]
df["Promoted"] = [True, False, True]
df["Marks"] = [82, 38, 63]
# Getting the dataframe as an output
print(df)

The code gives the following output.

   Name  Promoted  Marks
0  John      True     82
1   Doe     False     38
2  Bill      True     63

Once the DataFrame is prepared, we can check whether the DataFrame contains items or is empty by writing the code given below. For this, we can use two methods.

We can use the function present in Pandas df.emptyor we can len(df.index)check the length of the DataFrame using .

We have used Pandas attributes in the following examples df.empty.

if df.empty:
    print("DataFrame is empty!")
else:
    print("Not empty!")

Since we have inserted the data into the columns, the output must be Not empty!.

Not empty!

Now, let’s move on to INchecking if a column in a Pandas DataFrame exists using the check_if method. See the code below to see this function in action.

if "Promoted" in df:
    print("Yes, it does exist.")
else:
    print("No, it does not exist.")

The code gives the following output.

Yes, it does exist.

For greater clarity, this can also be written as if 'Promoted' in df.columns:, instead of just df.


Check if a column exists in Pandas using NOT INthe operator

Let's see how we can use NOT INthe property to perform the same operation. It operates in the opposite way, and because of the negation added to the property, the output is inverted.

This is a sample working of the property given below NOT IN.

if "Promoted" not in df.columns:
    print("Yes, it does not exist.")
else:
    print("No, it does exist.")

The code gives the following output.

No, it does exist.

We have seen how to do this for a single column in a DataFrame. Pandas also enables users to examine multiple columns in a DataFrame.

This helps in completing the task quickly and helps in sorting multiple columns at the same time.

Below is the code snippet to inspect multiple columns in a Pandas DataFrame.

if set(["Name", "Promoted"]).issubset(df.columns):
    print("Yes, all of them exist.")
else:
    print("No")

The code gives the following output.

Yes, all of them exist.

set([])It can also be constructed using curly braces.

if not {"Name", "Promoted"}.issubset(df.columns):
    print("Yes")
else:
    print("No")

The output will be:

No

These are the possible methods to check one or more columns in the data. Also, we can perform these functions on ready-made data instead of dummy data.

We just need read_csvto import the CSV file using the Python Pandas module through the import method. If you are using Google Colab, import google.colabthe filesmodule from to upload the data file from your personal system at runtime.

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.

Article URL:

Related Articles

Finding the installed version of Pandas

Publish Date:2025/04/12 Views:190 Category:Python

Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc

KeyError in Pandas

Publish Date:2025/04/12 Views:81 Category:Python

This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest

Grouping and Sorting in Pandas

Publish Date:2025/04/12 Views:90 Category:Python

This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies

Plotting Line Graph with Data Points in Pandas

Publish Date:2025/04/12 Views:65 Category:Python

Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data. Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize

Converting Timedelta to Int in Pandas

Publish Date:2025/04/12 Views:123 Category:Python

This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .

Pandas fill NaN values

Publish Date:2025/04/12 Views:93 Category:Python

This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values ​​with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50

Pandas Convert String to Number

Publish Date:2025/04/12 Views:147 Category:Python

This tutorial explains how to pandas.to_numeric() convert string values ​​of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :

How to Change the Data Type of a Column in Pandas

Publish Date:2025/04/12 Views:139 Category:Python

We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method

Get the first row of Dataframe Pandas

Publish Date:2025/04/12 Views:78 Category:Python

This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial