Sudhir Tiwari
Sudhir Tiwari

Reputation: 65

How to compare one column value to other columns of a large data set in pandas

I have a dataframe that is created by merging two tables. Now for each row i have to pick value from specific column and match it with other's column whose name provided in a list.

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"

It is showing following error

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

i even try x[li] but didn't work for me.

Upvotes: 0

Views: 1406

Answers (1)

piRSquared
piRSquared

Reputation: 294258

This will return a series where for each element of Col you'll get a truth value on whether or the not the entire column is equal to the 'Column_to_be_match'

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

This will return a dataframe of truth values comparing the 'Column_to_be_match' to every column in Col

df[Cols].eq(df['Column_to_be_match'], 0)

Upvotes: 1

Related Questions