Search code examples
pythonpandasdataframelistnested-lists

Finding the minimum value in lists inside a dataframe with TypeError: 'float' object is not iterable


I've generated a Dataframe with a column of lists, but if there are no values, there's a NaN instead.

import pandas as pd
df = pd.DataFrame(columns=['Lists', 'Min'])
df['Lists'] = [ [1,2,3], [4,5,6], [7,8,9], float('NaN') ]
print(df)

       Lists  Min
0  [1, 2, 3]  NaN
1  [4, 5, 6]  NaN
2  [7, 8, 9]  NaN
3        NaN  NaN

I would like for df['Min'] to contain the minimum value of the corresponding list in the same row. Thus:

       Lists  Min
0  [1, 2, 3]  1
1  [4, 5, 6]  4
2  [7, 8, 9]  7
3        NaN  NaN

However, when I try list comprehension I receive an error.

df['Min'] = [min(x) for x in df.Lists.tolist()]

Produces the error

TypeError: 'float' object is not iterable

How can I find the minimum of each list?


Solution

  • You can easily fix your list comprehension by checking if the value is NaN/iterable/a list:

    df['Min'] = [x if pd.isna(x) else x for min(x) in df.Lists.tolist()]
    
    df['Min'] = [min(x) if hasattr(x, '__iter__') else float('nan') for x in df.Lists.tolist()]
    
    df['Min'] = [min(x) if isinstance(x, list) else float('nan') for x in df.Lists.tolist()]
    

    Or use explode and groupby.min:

    df['Min'] = df['Lists'].explode().groupby(level=0).min()
    

    Output:

           Lists  Min
    0  [1, 2, 3]  1.0
    1  [4, 5, 6]  4.0
    2  [7, 8, 9]  7.0
    3        NaN  NaN