Search code examples
pythonpandasapply

pandas apply on a column (valus is of type set) to retrieve the first element pop results in error


The dataframe comprises of 2 columns, name (string) and key_set (set of string uuid)

df[df['key_set'].apply(lambda x: len(x) == 1)]['key_set'].apply(lambda x: x.pop())

Results in an error, which seems not possible as it is already filtered

pop from an empty set

where as the below works fine

df[df['key_set'].apply(lambda x: len(x) == 1)]['key_set'].apply(lambda x: next(iter(x)))

Solution

  • When you first run one of that expressions set.pop() will change a mutable set inplace, so that each other expression running with same set.pop() will throw the error KeyError: 'pop from an empty set' due to exhausted set object.

    Instead of redundant filtering, to collect the values and preserve the initial df with sets use the following approach (assuming that):

    df['key_set'].apply(lambda s: np.nan if len(s) != 1 else s.copy().pop()).dropna()