Skip to content
Advertisement

How to solve ValueError while checking rows in a particular column in pandas dataframe?

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?

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement