Remove duplicate rows based on three columns

clubkli

I have a data frame with prices of products on different dates. If there are same prices for a product on different dates, I would like to keep only the row with the most recent date.

Example of my data frame:

            Date           Price             Product
1         2019-08-28       10               product 1
2         2019-08-27       10               product 1
3         2019-08-28       15               product 2
4         2019-08-27       14               product 2
5         2019-08-23       15               product 2
6         2019-08-27       10               product 3

So I would like to get rid of row 2 and row 5 and only have:

            Date           Price             Product
1         2019-08-28       10               product 1
3         2019-08-28       15               product 2
4         2019-08-27       14               product 2
6         2019-08-27       10               product 3

Any suggestions? I could not find a question with a similar problem.

Ape

Order data by Date, then remove rows duplicated in the other two columns

df <- read.table(text = "          Date           Price             Product
1         2019-08-28       10               product1
                 2         2019-08-27       10               product1
                 3         2019-08-28       15               product2
                 4         2019-08-27       14               product2
                 5         2019-08-23       15               product2
                 6         2019-08-27       10               product3", 
                 header = TRUE, stringsAsFactors = FALSE)

df <- df[order(df[,1], decreasing = TRUE),]
df[!duplicated(df[,-1]),]

#        Date Price  Product
#1 2019-08-28    10 product1
#3 2019-08-28    15 product2
#4 2019-08-27    14 product2
#6 2019-08-27    10 product3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove duplicate rows based on content of three different columns

How to remove duplicate rows based on some columns

Remove duplicate rows based on all columns via VBA

Jquery: How to remove duplicate HTML TABLE rows based on columns values

Remove duplicate rows based on multiple columns using dplyr / tidyverse?

Remove duplicate column pairs, sort rows based on 2 columns

R - Identify and remove duplicate rows based on two columns

Remove duplicate rows based on 2 columns and a condition in a third column

SQL: Duplicate rows based on columns

TSQL Duplicate rows based on columns

R - Identify duplicate rows based on multiple columns and remove them based on date

Remove rows based on columns values

remove rows with conditions based on columns

R - remove duplicate rows (based on 2 columns, regardless of order) in data table

How do I remove duplicate rows based on two Columns? (E.g. A Pair Value Set)

Remove duplicate rows in one column based on another column and keep other columns intact

Remove duplicate rows from csv file based on 2 columns with regex in Python

Remove rows have duplicate value in two columns

Remove duplicate values in a range by rows not columns

How to remove duplicate columns as rows for pandas df

Removing rows based on duplicate columns in MySQL

Identify duplicate rows based on specifc columns

Duplicate rows in R based on content of columns

remove duplicate rows based on one column value

Remove duplicate based on 2 rows value

Remove duplicate rows in pandas dataframe based on condition

pyspark remove duplicate rows based on column value

Select rows and remove duplicate based on value of a column

Pandas Remove Duplicate Rows Based on Condition