【问题标题】:Check if one series is subset of another in Pandas检查一个系列是否是 Pandas 中另一个系列的子集
【发布时间】:2016-07-16 18:54:34
【问题描述】:

我有来自 2 个不同数据框的 2 列。我想检查第 1 列是否是第 2 列的子集。

我使用的是以下代码:

set(col1).issubset(set(col2))

这样做的问题是,如果 col1 只有整数,而 col2 同时有整数和字符串,则返回 false。发生这种情况是因为 col2 的元素被强制转换为字符串。例如,

set([376, 264, 365, 302]) & 
set(['302', 'water', 'nist1950', '264', '365', '376'])

我尝试使用熊猫的isin。但是如果 col1 和 col2 是系列,那么这会给出一系列布尔值。我要True or False

我该如何解决这个问题?有没有我错过的更简单的功能?

编辑 1

添加示例。

col1
0    365
1    376
2    302
3    264
Name: subject, dtype: int64

col2
0     nist1950
1     nist1950
2        water
3        water
4          376
5          376
6          302
7          302
8          365
9          365
10         264
11         264
12         376
13         376
Name: subject, dtype: object

编辑 2

col1 和 col2 可以有整数、字符串、浮点数等。我不想对这些列中的内容做出任何预先判断。

【问题讨论】:

  • 你可以使用col1.isin(col2).all()
  • 我还是错了。

标签: python pandas subset


【解决方案1】:

您可以使用isinall 来检查您的所有col1 元素是否包含在col2 中。要转换为数字,您可以使用 pd.to_numeric:

s1 = pd.Series([376, 264, 365, 302])
s2 = pd.Series(['302', 'water', 'nist1950', '264', '365', '376'])

res = s1.isin(pd.to_numeric(s2, errors='coerce')).all()

In [213]: res
Out[213]: True

更详细:

In [214]: pd.to_numeric(s2, errors='coerce')
Out[214]:
0    302
1    NaN
2    NaN
3    264
4    365
5    376
dtype: float64

In [215]: s1.isin(pd.to_numeric(s2, errors='coerce'))
Out[215]:
0    True
1    True
2    True
3    True
dtype: bool

注意 pd.to_numeric 与 pandas 版本 >=0.17.0 一起使用,以前你可以使用 convert_objectsconvert_numeric=True

编辑

如果您更喜欢set 的解决方案,您也可以将您的第一组转换为str,然后将它们与您的代码进行比较:

s3 = set(map(str, s1))

In [234]: s3
Out[234]: {'264', '302', '365', '376'}

那么你可以使用issubset 代替s2

In [235]: s3.issubset(s2)
Out[235]: True

set(s2)

In [236]: s3.issubset(set(s2))
Out[236]: True

EDIT2

s1 = pd.Series(['376', '264', '365', '302'])
s4 = pd.Series(['nist1950', 'nist1950', 'water', 'water', '376', '376', '302', '302', '365', '365', '264', '264', '376', '376'])

In [263]: s1.astype(float).isin(pd.to_numeric(s4, errors='coerce')).all()
Out[263]: True

【讨论】:

  • 我仍然得到错误 col1 0 365 1 376 2 302 3 264 名称:主题,dtype:int64 col2 0 nist1950 1 nist1950 2 water 3 water 4 376 5 376 6 302 7 302 8 365 9 365 10 264 11 264 12 376 13 376 姓名:主体,数据类型:客体
  • @Swetabh 你可以为你的问题添加这个例子吗?
  • 刚刚做了。让我知道这是否足够。
  • @Swetabh 对我来说很好用...添加了str 的示例s1。您需要为此使用astype。或者你也可以使用pd.to_numeric
  • 当与 pd.to_numeric 一起使用时确实如此。问题是我不确定 col1 或 col2 是否会一直这样。在其他情况下,可能是 col1 有字符串,而 col2 没有。我如何以一般方式处理这个问题?我是否应该始终将两列都转换为字符串/数字,然后进行比较?
【解决方案2】:

您可以将merge 与参数indicator=True 一起使用:

In [3]:
df1 = pd.DataFrame({'a':[376, 264, 365, 302]})
df2=pd.DataFrame({'b':[302, 'water', 'nist1950', '264', '365', '376']})
df1.merge(df2, left_on='a', right_on='b', how='left',indicator=True)

Out[3]:
     a    b     _merge
0  376  NaN  left_only
1  264  NaN  left_only
2  365  NaN  left_only
3  302  302       both

因此,如果您将感兴趣的列作为left_onright_on 参数传递,那么添加的列_merge 将告诉dfs 或left_only 中存在哪些列值

这需要pandas版本0.17.0及以上

【讨论】:

  • 你觉得这个方法比上面Anton给出的方法更通用吗?
  • 这允许您使用仅左侧、仅右侧、外部等指定条件。此外,您还可以传递多个列
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 1970-01-01
  • 2021-01-04
  • 2013-03-31
  • 2023-03-18
  • 1970-01-01
  • 2021-02-21
相关资源
最近更新 更多