![](/img/trans.png)
[英]How to compare a dataframe with another df and return new rows from the first df using pandas
[英]How to compare one dataframe with another and check whether the same data in first df present in second df
有兩個 df.both df 的一列和相同的名稱
df1 有 40000 行,df 2 有 80000 行。
如何比較df1中的數據與df2中的數據是否相同。
預期輸出:任何說明 df1 中的 40000 行與 df2 匹配的消息,其中 df2 有 80000 行
40000 items in df1 matched with 80000 items in df2
就像是:
m = df1['c'] == df2['c']
print('{0:d} items in df1 matched with {1:d} items in df2'.format(sum(m), len(m)))
用這個:
match = df1[df1['column name'].isin(df2['column name'])].shape[0]
print(('%.i items matched') % match)
嘗試:
matches = (df2 == df1).stack()
df = pd.DataFrame(data1, columns = ['A'])
df2 = pd.DataFrame(data2, columns = ['A'])
df
A
0 10
1 15
2 14
3 20
4 25
5 26
df2
A
0 10
1 15
2 14
3 20
4 25
5 26
6 30
7 32
8 34
9 36
df2[df2.A.isin(df.A.values)]
A
0 10
1 15
2 14
3 20
4 25
5 26
匹配元素
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.