我有两个数据框,如:

df:

    a       b         c      d
0  12   "vik"   [9,  18]   "SS"
1  13   "Rah"   [10, 18]   "YY"


df2:

    a       b         c      d
0  12   "vik"   [9,  18]   "SS"
1  13   "Rah"   [10, 18]   "YY"
2  14   "Dil"   [11, 18]   "ZZ"


我想消除df2中df2中的行。我试过了

df2.sub(df, fill_values=0)


这给我一个错误TypeError: unsupported operand type(s) for -: 'str' and 'str'

我想要的输出是这样的:

    a       b         c      d
0  14   "Dil"   [11, 18]   "ZZ"


任何帮助都是可观的。

最佳答案

merge与左联接和参数indicator=True一起使用,然后按query过滤并删除列_merge

df1['c'] = df1['c'].apply(tuple)
df2['c'] = df2['c'].apply(tuple)

df3 = (df2.merge(df, how='left', indicator=True)
          .query('_merge == "left_only"')
          .drop('_merge', axis=1))

df3['c'] = df3['c'].apply(list)
print (df3)
    a    b         c   d
2  14  Dil  [11, 18]  ZZ

关于python - Pandas 通过非数值减去两个数据帧,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52848526/

10-13 02:20