代码之家  ›  专栏  ›  技术社区  ›  Vikas Periyadath Ramana Sriwidya

熊猫用非数值减去两个数据帧

  •  1
  • Vikas Periyadath Ramana Sriwidya  · 技术社区  · 6 年前

    测向:

        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中df中的行。我试过了

    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"
    

    任何帮助都是可观的。

    2 回复  |  直到 6 年前
        1
  •  2
  •   jezrael    6 年前

    使用 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
    
        2
  •  1
  •   Rakesh    6 年前

    concat drop_duplicates

    前任:

    import pandas as pd
    
    df = pd.DataFrame({"a": [12, 13], "b":["vik", "Rah"], "c":[[9,  18], [10, 18]], "d":["SS", "YY"]})
    df2 = pd.DataFrame({"a": [12, 13, 14], "b":["vik", "Rah", "Dil"], "c":[[9,  18], [10, 18], [11, 18]], "d":["SS", "YY", "ZZ"]})
    
    df3 = pd.concat([df, df2], ignore_index=True)
    df3["c"] = df3["c"].apply(tuple)
    print(df3.drop_duplicates(keep=False))