Search code examples
pythonpandaslistdataframeintersection

For finding intersection: TypeError: 'float' object is not iterable:


Hi i am working with Pandas and have my two column, i wanted to calculate the Intersection of two columns , enter image description here

When I am trying to :

df9['C'] = [len(set(a).intersection(b)) for a, b in zip(df9.Topic1_assignment, df9.Topic2_assignment)]

I get a TypeError: 'float' object is not iterable, What is the possible reason for this error?

Here is my data: https://github.com/mayuripandey/Data-Analysis/blob/main/Topic.csv


Solution

  • You have to process nan values differently because set(np.nan) raises a TypeError.

    You can use:

    df9['c'] = [len(set(a).intersection(b)) if all(pd.notna([a, b])) else 0
                    for a, b in zip(df9.Topic1_assignment, df9.Topic2_assignment)]