2017-09-01 41 views
0

我有一個通過合併兩個表創建的數據框。現在,對於每一行,我都必須從特定列中選擇值,並將其與列表中名稱提供的其他列匹配。如何比較一個列值與熊貓中的大型數據集的其他列

def segmentMatch(str1,str2): 
if(str1==str2): 
    return 1 
else: 
    return 0 

Cols=['Col1','Col2','Col3','Col4','Col5'.....,'Col20'] 
for li in Cols: 
    #print li 
    if (df.apply(lambda x: segmentMatch(x['Column_to_be_match'], x.li), axis=1)): 
     print "Matched" 

它顯示以下錯誤

AttributeError: ("'Series' object has no attribute 'li'", u'occurred at index 0', u'occurred at index 0') 

我甚至嘗試X [李],但對我沒有工作。

回答

1

這將返回一個系列賽裏爲Col每個元素你會得到整列是否未等於'Column_to_be_match'

df[Cols].apply(pd.Series.equals, other=df['Column_to_be_match']) 

這將返回真值的數據幀真值將'Column_to_be_match'Col

df[Cols].eq(df['Column_to_be_match'], 0) 
相關問題