pandas 对列应用 apply(值为 set 类型)以检索第一个元素会导致错误。

huangapple go评论61阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年2月6日 18:26:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定