簡體   English   中英

Pandas通過非數字值減去兩個數據幀

[英]Pandas subtract two data frames by non-numeric values

我有兩個數據框,如:

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"

我想從df中消除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"

任何幫助都很明顯。

使用左連接和參數indicator=True merge ,然后按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

這是使用concatdrop_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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM