英文:
pandas apply on a column (valus is of type set) to retrieve the first element pop results in error
问题
以下是您要的代码部分的中文翻译:
数据框包括2列,name(字符串)和key_set(字符串UUID的集合)
# 下面的代码导致错误,似乎不可能,因为已经进行了筛选
# 错误消息为:"从空集合弹出"
# 以下的代码工作正常
英文:
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)))
答案1
得分: 1
当您首次运行其中一个表达式set.pop()
时,将会在原地更改可变的set,因此,每个使用相同set.pop()
的其他表达式都会因已耗尽的集合对象而引发错误KeyError: 'pop from an empty set'
。
不要使用冗余过滤,而是使用以下方法来收集值并保留初始的df
与集合(假设如下):
df['key_set'].apply(lambda s: np.nan if len(s) != 1 else s.copy().pop()).dropna()
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论