我將如何撰寫一個函式來檢測是否存在重復的熊貓資料框。因此,如果我比較index列之間first并且second沒有重復項。但是,如果我比較index之間的列,first并且third有重復的1. 我想撰寫一個函式,bool當有重復項時回傳 True ,沒有重復項時回傳 True False。
import pandas as pd
first = pd.DataFrame({'index': [1,4,5,6],
'vals':[3,4,5,7] })
second = pd.DataFrame({'index': [13,7,8,9],
'vals':[3,2,3,1] })
third = pd.DataFrame({'index': [1,11,2,12],
'vals':[6,7,51,2] })
預期輸出:
first and second: False
first and third: True
uj5u.com熱心網友回復:
使用sets謂詞:
>>> any(set(first['index']).intersection(second['index']))
False # because {}
>>> any(set(first['index']).intersection(third['index']))
True # because {1}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/431723.html
