Contra111
Contra111

Reputation: 327

pandas check last groupbyed element

this question is kinda similar to Pandas count groupbyed elemenys by condition

I have dataframe like this:

df = pd.DataFrame({
    'user': ['1', '1', '1', '2', '2', '2', '3', '3', '3'],
    'value': ['4', '4', '1', '2', '2', '2', '3', '1', '1']
})

'value' sorted by date, so i need to count users for which the last element is equal to something, for example '1' (it should be string)

for this df it would be 2 because last element for group 'user 1' is '1', same thing for user 3, but user 2 last element is not '1' so i dont need to count it

Upvotes: 1

Views: 43

Answers (3)

Balaji Ambresh
Balaji Ambresh

Reputation: 5012

Could you give this a shot?

df.groupby('user', sort=False).value.apply(lambda vals: vals.iloc[-1] == '1').sum()

Output:

2

Upvotes: 1

jezrael
jezrael

Reputation: 863531

Use DataFrame.drop_duplicates with keep='False' for last rows of groups by user, then check value for equality by Series.eq and count Trues values by sum:

out = df.drop_duplicates('user', keep='last')['value'].eq('1').sum()
print(out)
2

Another idea with GroupBy.last:

out = df.groupby('user')['value'].last().eq('1').sum()
print(out)
2

Upvotes: 2

EddyG
EddyG

Reputation: 685

Just get the last value with a groupby and compare

df.groupby('user').apply(lambda x:  True if x['value'].iloc[-1]=='1' else False).sum()

Upvotes: 1

Related Questions