5

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.

2
  • No it is not the duplicate.. There we are getting rows having same values across all the columns. But here i want only for particular few columns. Commented May 18, 2019 at 12:13
  • It doesn't matter. Selecting columns is a trivial step and not worth disputing closure over. Commented May 19, 2019 at 5:52

4 Answers 4

11

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
Sign up to request clarification or add additional context in comments.

Comments

0

Try

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

Comments

0

Please see attached

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

Comments

0

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)
     

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.