Liam Pieri
Liam Pieri

Reputation: 643

Select rows for which values are same in select columns Pandas?

I'm trying to take the subset of my dataframe that has the same values for an arbitrary amount of columns.

Something like

df[df['col1'] == df['col2'] ==....] 

But for an arbitrary amount of column names.

Upvotes: 3

Views: 1159

Answers (2)

jpp
jpp

Reputation: 164643

Inspired by @Jinglesting's solution:

df.T.apply(set).map(len) <= 1

Upvotes: 2

Jinglesting
Jinglesting

Reputation: 517

I think this should work

columns_to_compare = ["foo", "bar"]

def check_all_equal(iterator):
   return len(set(iterator)) <= 1

df[df[columns_to_compare].apply(lambda row: check_all_equal(row), axis=1)]

Upvotes: 3

Related Questions