Home > other >  DataFrame, how to choose NAN as if condition
DataFrame, how to choose NAN as if condition

Time:05-11

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({"a":[3,5,2,4,7],"b":[14,13,14,16,15],"c":[np.nan,'Bob',np.nan,'Brad',np.nan]})

for row in df.iterrows():
    if df.c.isnull():
        plt1=plt
        plt1.scatter(df.a,df.b)
        plt1.show()

I have a DataFrame, and I want to choose the c column as if statement condition, if c is NAN, then plot (a,b), there is error when the code run:ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). How to change the code. Thanks!

CodePudding user response:

Use isna() to get all the null values in a pandas series.

null_df = df.loc[df["c"].isna() == True, df.columns[:-1]]
plt.figure(figsize = (20,5))
plt.scatter(null_df["a"], null_df["b"])
plt.show()

Output -

enter image description here

CodePudding user response:

So i think you can try do just something like this:


for index, isna in enumerate(df.c.isnull()):
    if isna:
        plt.scatter(df.a[index], df.b[index])
        plt.show()

Tell me if it's what you desire. Thanks

  • Related