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

pandas select rows where two columns are equal
pandas select columns by condition
pandas select rows by value
pandas check if two columns are identical
find duplicates based on multiple columns pandas
select rows based on multiple column value pandas
pandas duplicate rows based on column value
pandas dataframe lookup multiple columns

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.

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

Using iloc, loc, & ix to select rows and columns in Pandas DataFrames, The iloc, loc and ix indexers for Python Pandas select rows and columns from DataFrames. data.iloc[:, 0:2] # first two columns of data frame with all rows row with index value 487) is not equal to data.iloc[487] (the 487th row in the data) . Let’s see how to Select rows based on some conditions in Pandas DataFrame. Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator.. Code #1 : Selecting all the rows from the given dataframe in which ‘Percentage’ is greater than 80 using basic method.

Try

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

Indexing and Selecting Data — pandas 0.13.1 documentation, Getting values from an object with multi-axes selection uses the following notation (using .loc If a column is not contained in the DataFrame, an exception will be raised. Another common operation is the use of boolean vectors to filter the data. get all rows where columns "a" and "b" have overlapping values In [ 161]: df� Pandas : Find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicated() in Python Select Rows & Columns by Name or Index in DataFrame using loc & iloc | Python Pandas

Please see attached

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

Indexing and selecting data — pandas 0.8.1 documentation, This allows you to select rows where one or more columns have values you want: to identify and remove duplicate rows in a DataFrame, there are two methods wish get a subset (or analogously set a subset) of the data in a way that is not indexing and how it integrates with the all of the pandas indexing functionality� Find Duplicate Rows based on all columns. To find & select the duplicate all rows based on all columns call the Daraframe.duplicate() without any subset argument. It will return a Boolean series with True at the place of each duplicated rows except their first occurrence (default value of keep argument is ‘first’). Then pass this Boolean Series to [] operator of Dataframe to select the rows which are duplicate i.e.

pandas.DataFrame.assign — pandas 1.1.0 documentation, DataFrame.iterrows � pandas.DataFrame.itertuples DataFrame.get � pandas. Returns a new object with all original columns in addition to new ones. Existing If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned. Returns Assigning multiple columns within the same assign is possible. Python | Delete rows/columns from DataFrame using Pandas.drop() Apply a function to single or selected columns or rows in Pandas Dataframe; Find maximum values & position in columns and rows of a Dataframe in Pandas; Get minimum values in rows or columns with their index position in Pandas-Dataframe

Find duplicate rows in a Dataframe based on all or selected columns , If 'False', This considers all of the same values as duplicates. Returns: Boolean Series denoting duplicate rows. Let's create a simple dataframe with a dictionary of� If DataFrames have exactly the same index then they can be compared by using np.where. This will check whether values from a column from the first DataFrame match exactly value in the column of the second: import numpy as np df1['low_value'] = np.where(df1.type == df2.type, 'True', 'False') result:

Pandas : Get unique values in columns of a Dataframe in Python , First of all, create a dataframe, Get a series of unique values in column 'Age' of the dataframe Using nunique() with default arguments doesn't include NaN while To get the unique values in multiple columns of a dataframe, we can Pandas : Check if a value exists in a DataFrame using in & not in� When using GROUP BY in your query, order the columns by the cardinality by the highest cardinality (that is, most number of unique values, distributed evenly) to the lowest. shuffle. When there is different names tables in the columns, The following syntax can be used: Dec 20, 2017 · Rename multiple pandas dataframe column names. 1, Column 1.

Comments
  • 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.
  • It doesn't matter. Selecting columns is a trivial step and not worth disputing closure over.