【问题标题】:What is the analogue of EXCEPT clause in SQL in Pandas?Pandas 中 SQL 中的 EXCEPT 子句的类比是什么?
【发布时间】:2017-05-19 22:11:45
【问题描述】:

我有一个示例 pandas 数据框 df:

        col1    col2    col3    col4
      0   a      1.0    2.0      3
      1   b      NaN    NaN      6
      2   c      NaN    8.0      9
      3   d      NaN    11.0    12
      4   e     13.0    14.0    15
      5   f     17.0    18.0    19
      6   g     21.0    22.0    23

还有第二个df1:

        col1    col2    col3    col4
      0  a      1.0     2.0      3
      4  e     13.0    14.0     15
      5  f     17.0    18.0     19
      6  g     21.0    22.0     23

我想得到不与 df1 重叠的 df 子集。实际上,我正在寻找 SQL 中 EXCEPT 操作数的等价物。

我使用了减法() 函数——但这显然是错误的,因为减法执行的是元素数值减法。所以我收到一条错误消息:

       TypeError: unsupported operand type(s) for -: 'str' and 'str'

所以问题是:对于 Pandas,SQL 中 EXCEPT 的等价物是什么?

【问题讨论】:

  • 那么错误很明显,你不能减去字符串,除了你没有发布所需的输出应该是什么。

标签: python sql pandas except


【解决方案1】:

我认为您首先需要所有字符串列中的set_index:

df2 = df.set_index('col1').subtract(df1.set_index('col1'), axis='columns')
print (df2)
      col2  col3  col4
col1                  
a      0.0   0.0   0.0
b      NaN   NaN   NaN
c      NaN   NaN   NaN
d      NaN   NaN   NaN
e      0.0   0.0   0.0
f      0.0   0.0   0.0
g      0.0   0.0   0.0

或者:

df2 = df.set_index('col1').subtract(df1.set_index('col1'), axis='columns', fill_value=0)
print (df2)
      col2  col3  col4
col1                  
a      0.0   0.0   0.0
b      NaN   NaN   6.0
c      NaN   8.0   9.0
d      NaN  11.0  12.0
e      0.0   0.0   0.0
f      0.0   0.0   0.0
g      0.0   0.0   0.0

编辑问题:

print (df.isin(df1))
    col1   col2   col3   col4
0   True   True   True   True
1  False  False  False  False
2  False  False  False  False
3  False  False  False  False
4   True   True   True   True
5   True   True   True   True
6   True   True   True   True

print (df.isin(df1).all(axis=1))
0     True
1    False
2    False
3    False
4     True
5     True
6     True
dtype: bool

print (~df.isin(df1).all(axis=1))
0    False
1     True
2     True
3     True
4    False
5    False
6    False
dtype: bool

print (df[~(df.isin(df1).all(axis=1))])
  col1  col2  col3  col4
1    b   NaN   NaN     6
2    c   NaN   8.0     9
3    d   NaN  11.0    12

【讨论】:

  • 谢谢。现在我意识到我使用了错误的功能。我对元素数值减法运算不感兴趣,而是对 SQL 中的 EXCEPT 子句执行的操作感兴趣。 IE。我想获得不与 df1 重叠的 df 子集。你知道我该怎么做吗?
  • 看来您需要print (df[~(df.isin(df1).all(axis=1))]) - 但必须按索引对齐。
  • 它删除所有具有相同值和相同索引 drom df 的行。
【解决方案2】:

我认为与 SQL EXCEPT (MINUS) 等效的 Pandas 将是以下技术:

In [16]: df1
Out[16]:
   a  b  c
0  1  a  5   # duplicates row with index: 3 
1  0  x  4
2  9  Z  9   # exists in DF2, so it should NOT appear in the result set
3  1  a  5   # duplicates row with index: 3

In [17]: df2
Out[17]:
    a  b    c
0  66  a  5.0
1   9  Z  9.0
2   0  x  NaN

In [18]: (pd.merge(df1, df2, on=df1.columns.tolist(), how='outer', indicator=True)
    ...:    .query("_merge == 'left_only'")
    ...:    .drop('_merge', 1)
    ...: )
    ...:
Out[18]:
   a  b    c
0  1  a  5.0
1  1  a  5.0
2  0  x  4.0

注意:此解决方案不关注索引

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2011-09-20
    • 2018-08-22
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多