ssp
ssp

Reputation: 71

Find rows having same values in multiple columns(Not All Columns) in Pandas Dataframe

Below is my Dataframe:

X1  X2  X3  X4  X5
A   B   C   10  BAM
A   A   A   12  BAM
B   B   B   10  BAM
A   B   B   60  BAM

I want those rows having same values in columns(X1, X2,X3). Here we can see 2nd and 3rd rows are having same values for above 3 columns. My desired output is:

 X1 X2  X3  X4  X5
A   A   A   12  BAM
B   B   B   10  BAM

I tried like below:

yourdf1=df[df.nunique(0)==0]
print(yourdf1)

But here i am getting an error. Could anyone please help me.

Upvotes: 5

Views: 12872

Answers (4)

Ati Srv
Ati Srv

Reputation: 36

You can iterate over each row and compare columns with each other, and attach the rows which are the same to a new dataframe. The code would look something like this:

df2 = pd.DataFrame()
for row in df.rows:
    if (row['X1'] == row['X2']  and row['X2'] == row['X3']):
       df2.append(row)
display(df2)
     

Upvotes: 0

CodingHusky
CodingHusky

Reputation: 1

Please see attached

df[df[['X1','X2','X3']].duplicated(keep=False)]

Upvotes: 0

Quang Hoang
Quang Hoang

Reputation: 150735

Try

yourdf = df[~df.duplicated(subset=['X1','X2','X3'])]

Upvotes: 0

jezrael
jezrael

Reputation: 862511

Select columns in list for test number of unique values per rows by axis=1 in DataFrame.nunique and test 1 for filter by boolean indexing:

yourdf1 = df[df[['X1','X2','X3']].nunique(axis=1) == 1]
print(yourdf1)
  X1 X2 X3  X4   X5
1  A  A  A  12  BAM
2  B  B  B  10  BAM

Another solution is use DataFrame.eq with filtered DataFrame, compare by first column and get all Trues per rows by DataFrame.all:

df1 = df[['X1','X2','X3']]
yourdf1 = df[df1.eq(df1.iloc[:, 0], axis=0).all(axis=1)]
print(yourdf1)

  X1 X2 X3  X4   X5
1  A  A  A  12  BAM
2  B  B  B  10  BAM

Upvotes: 11

Related Questions