Home > Software engineering >  How to solve ValueError while checking rows in a particular column in pandas dataframe?
How to solve ValueError while checking rows in a particular column in pandas dataframe?

Time:02-15

I'm trying to get number of "NaN" values in particular column using below code. I can't use df["column_name"].isna().sum() because i have thousands and column and i want to check number of null values in each column. Sometimes i also need to check symbols presents in the column.

count=0
for col, rows in df.items():
    if col == "co2":
        if rows=="NaN":
            count=count 1
print(count)

Every time i run this code, i gets this ValueError saying following things.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Can anyone help to find what is wrong with this code?

CodePudding user response:

You can use a list of all the columns you want to count the null values for and iterate over that list to find the nan counts. As an example if you want to go over all of the dataframe's columns:

columns = df.columns.values.tolist()
nan_count = {}
for column in columns:
    nan_count[column] = df[column].isna().sum()

CodePudding user response:

The problem is told you by Python. To make this code work you should amend your condition to

if "NaN" in list(rows):
    print("got it")

but it is very inefficient with large datasets. The approach you are excluding should be employed.

  • Related