Skip to content Skip to sidebar Skip to footer

Find The Common Values In Columns In Pandas Dataframe

I have the data frame of the style: animal animal A Dog Dog B Cat Cat C Pig Pig D Cat Dog The different entries in row D tell me there is an

Solution 1:

The preparatory code generates a dataframe with the same structure as yours. Interestingly, I was unable to name the columns animal and join with suffix = ("","") -- that throws an error ValueError: columns overlap but no suffix specified: Index([u'animal'], dtype='object'). @chrisaycock's comment to rename the columns works just fine.

import pandas as pd

# prepare the dataframe
a1 = ['Dog','Cat','Pig','Cat']
a2 = ['Dog','Cat','Pig','Dog']
df1 = pd.DataFrame({"ani": a1})
df2 = pd.DataFrame({"ani": a2})
# trickery required to get two columns with the same name
df = pd.merge(df1, df2, left_index=True,right_index = True, suffixes=("mal", "mal"))

# fix the column names
df.columns = ['animal1', 'animal2']

# keep only matching rows
df = df[df.animal1 == df.animal2]

Solution 2:

Iterating through the data frame based on the index and checking for return value.

>>>for i in df.index:...iflen(set(df.ix[i])) != 1:...            df.drop(i)

Post a Comment for "Find The Common Values In Columns In Pandas Dataframe"