Filter column for multiple values but only select the last one for one criteria

ThatQuantDude

I have a dataframe similar to this one

df = pd.DataFrame({'date':[20220101,20220102,20220103,20220101,20220102,20220101], 'id':[1,1,1,2,2,3], 'value':[11,22,33,44,55,66], 'categorie':['a','a','c','a','c','c']})

       date  id  value categorie
   20220101   1     11         a
   20220102   1     22         a
   20220103   1     33         c
   20220101   2     44         a
   20220102   2     55         c
   20220101   3     66         c

I would now like to slice the df based on multiple values from column 'categorie' and am currently using

df = df[df['categorie'].isin(['a','c'])]

In addition to that I would like to be able to only get the [-1] row back for categorie 'a'

    date  id  value categorie
20220102   1     22         a
20220103   1     33         c
20220101   2     44         a
20220102   2     55         c
20220101   3     66         c

instead of

    date  id  value categorie
20220101   1     11         a
20220102   1     22         a 
20220103   1     33         c
20220101   2     44         a
20220102   2     55         c
20220101   3     66         c

I think the closest would be to think about it as a groupby max value on id and categorie but I am curious if there is a more pythonic way.

Naveed

'a' and 'c' are the only categories in your data, if you just need the latest then drop the duplicates

# drop duplicates and keep the last
df.drop_duplicates(subset=['id','categorie'], keep='last')

or

# select the categories of 'a' and 'c' and drop the duplicates from among them
(df.loc[df['categorie'].isin(['a','c'])]
 .drop_duplicates(subset=['id','categorie'], keep='last'))
    date       id   value   categorie
1   20220102    1      22   a
2   20220103    1      33   c
3   20220101    2      44   a
4   20220102    2      55   c
5   20220101    3      66   c

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Excel Filter, Multiple criteria, one column, both criteria true

Altair interactive filter on multiple values of one column

Group by multiple criteria in one select

filtering multiple values based on one matching criteria in a different column in excel

How i can filter one column by multiple values (strict filter)?

Laravel multiple select filter for categories, only get one select as output

SELECT last row, but only if column value is the expected one

Filter on multiple criteria defined in one cell

Sum values in one column of all rows which match one or more of multiple criteria

How to filter rows with select values from one column but with common ids?

Filter JTable only one one column

Pandas: filter one dataframe by multiple, simultaneous column values of another dataframe

VLOOKUP with multiple criteria returning values in one cell

select rows if all meets same criteria and only one meets a criteria

How to select unique values looking at only one column in SQL Server?

MySQL One row select only column without NULL values

How to select only duplicate values, but with one different column?

MS Access: query only one criteria in multiple criteria lookup field

How to select multiple columns grouping by with only one column

Select multiple columns with only one distinct column in sql

Select multiple columns but apply Avg() with only one column?

How to save only one column in a SELECT statement with multiple fields?

MySQL: Select multiple max values from one column

How to select distinct based on multiple values for one column

MySql: select values from one column into multiple columns in the result

How to select rows with multiple values i one column?

MYSQL: Select Query with multiple values from one column

SQL Select multiple values in one column, with different conditions

How to select multiple values in one column many to many relationship