【问题标题】:How can I get intersection of two pandas series text column?如何获得两个熊猫系列文本列的交集?
【发布时间】:2019-08-17 04:15:25
【问题描述】:

我有两个 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'

【问题讨论】:

  • 我试图以我自己的方式回答这个问题,保留你在问题中提到的观点。 dfdf2 是我根据名称猜测的数据帧,但答案是根据系列。所以我也想用intersection()来回答使用DataFrame。

标签: python python-3.x pandas


【解决方案1】:

看起来很简单的逻辑:

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

【讨论】:

  • 谢谢。我怎样才能与上述方法进行联合?
【解决方案2】:

这种方法适合我

import pandas as pd
import numpy as np

data = np.array([{'this', 'is', 'good'},{'this', 'is', 'not', 'good'}])
data1 = np.array([{'this', 'is'},{'good', 'bad'}])
df = pd.Series(data)
df1 = pd.Series(data1)

df2 = pd.Series([df[i] & df1[i] for i in xrange(df.size)])
print(df2)

【讨论】:

  • 它工作得很好,但在我庞大的数据集上运行缓慢。谢谢
【解决方案3】:

我很欣赏上述答案。如果您有 DataFrame,这是一个解决相同问题的简单示例(我猜,在查看 dfdf1 之类的变量名称后,您曾要求 DataFrame .).

这个df.apply(lambda row: row[0].intersection(df1.loc[row.name][0]), axis=1) 会这样做。让我们看看我是如何找到解决方案的。

https://stackoverflow.com/questions/266582... 的回答对我很有帮助。

>>> import pandas as pd

>>> 
>>> df = pd.DataFrame({
...     "set": [{"this", "is", "good"}, {"this", "is", "not", "good"}]
... })
>>> 
>>> df
                     set
0       {this, is, good}
1  {not, this, is, good}
>>> 
>>> df1 = pd.DataFrame({
...     "set": [{"this", "is"}, {"good", "bad"}]
... })
>>> 
>>> df1
           set
0   {this, is}
1  {bad, good}
>>>
>>> df.apply(lambda row: row[0].intersection(df1.loc[row.name][0]), axis=1)
0    {this, is}
1        {good}
dtype: object
>>> 

我是如何达到上述解决方案的?

>>> df.apply(lambda x: print(x.name), axis=1)
0
1
0    None
1    None
dtype: object
>>> 
>>> df.loc[0]
set    {this, is, good}
Name: 0, dtype: object
>>> 
>>> df.apply(lambda row: print(row[0]), axis=1)
{'this', 'is', 'good'}
{'not', 'this', 'is', 'good'}
0    None
1    None
dtype: object
>>> 
>>> df.apply(lambda row: print(type(row[0])), axis=1)
<class 'set'>
<class 'set'>
0    None
1    None
dtype: object
>>> df.apply(lambda row: print(type(row[0]), df1.loc[row.name]), axis=1)
<class 'set'> set    {this, is}
Name: 0, dtype: object
<class 'set'> set    {good}
Name: 1, dtype: object
0    None
1    None
dtype: object
>>> df.apply(lambda row: print(type(row[0]), type(df1.loc[row.name])), axis=1)
<class 'set'> <class 'pandas.core.series.Series'>
<class 'set'> <class 'pandas.core.series.Series'>
0    None
1    None
dtype: object
>>> df.apply(lambda row: print(type(row[0]), type(df1.loc[row.name][0])), axis=1)
<class 'set'> <class 'set'>
<class 'set'> <class 'set'>
0    None
1    None
dtype: object
>>> 

【讨论】:

    【解决方案4】:

    与上述类似,除非您想将所有内容都保存在一个数据帧中

    Current df:
    df = pd.DataFrame({0: np.array([{'this', 'is', 'good'},{'this', 'is', 'not', 'good'}]), 1: np.array([{'this', 'is'},{'good', 'bad'}])})
    
    Intersection of series 0 & 1
    df[2] = df.apply(lambda x: x[0] & x[1], axis=1)
    

    【讨论】:

      猜你喜欢
      • 2019-09-16
      • 2014-10-20
      • 2019-04-11
      • 2021-07-06
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多