gpt4 book ai didi

python - 如何获得两个 pandas 系列文本列的交集?

转载 作者:行者123 更新时间:2023-11-30 21:57:08 25 4
gpt4 key购买 nike

我有两个 pandas 系列的文本列,如何获得它们的交集?

print(df)

0 {this, is, good}
1 {this, is, not, good}

print(df1)

0 {this, is}
1 {good, bad}

我正在寻找如下所示的输出。

print(df2)

0 {this, is}
1 {good}

我已经尝试过,但它返回

df.apply(lambda x: x.intersection(df1))
TypeError: unhashable type: 'set'

最佳答案

看起来是一个简单的逻辑:

s1 = pd.Series([{'this', 'is', 'good'}, {'this', 'is', 'not', 'good'}])
s2 = pd.Series([{'this', 'is'}, {'good', 'bad'}])
s1 - (s1 - s2)
#Out[122]:
#0 {this, is}
#1 {good}
#dtype: object

关于python - 如何获得两个 pandas 系列文本列的交集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55362925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com