Python - How to iterate through a list inside a dataframe column skiping the nan

I need to iterate through a list inside a dataframe’s column. The problem is that sometimes I have missing values that make me get the error: “float' object is not iterable”. I don’t want to replace de missing values, so I need to skip it. I have a hug dataframe, but I show you just a little example:

dic = {"E1x":[19.2, 20.7,5.2, 6.5,-2],"E2x": [-9,3,2.5,-4,5],"E1y":[-1,2,5,7,8], "E2y":[3,5,17,-20,15], "Ata":[[23,10], [5,7,3], [20,23], np.nan, [2,5]]}
df=pd.DataFrame(dic)

for i, row in df.iterrows():
    if row['Ata'] is not None:
        for e in row["Ata"]:
            print(e)

How can I do to iterate through the column “Ata” skiping the NaN?

Upvotes: 1

Views: 386

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195543

You can use boolean-indexing:

mask = df["Ata"].notna()

for i, row in df[mask].iterrows():
    for e in row["Ata"]:
        print(e)

Prints:

23
10
5
7
3
20
23
2
5

EDIT: To check if the value is list:

for i, row in df.iterrows():
    if isinstance(row["Ata"], list):
        for e in row["Ata"]:
            print(e)

Upvotes: 1

Related Questions