Anonymous
Anonymous

Reputation: 477

How to reduce some values from a list within each row in a dataframe

I wish to cut out some of the values from a list in each row for every column by certain value.

b = pd.DataFrame({"a":[1,1,1,2,2],
                 "b":[6,7,8,9,10]})

b.groupby("a")["b"].apply(list).apply() # I try to group it but I do not know how to finish it after that. Any idea how to continue from this code? 

My idea is group all the "a" and then cut out one dataset or one row from each group.

    a   b
0   1   6
1   1   7
2   1   8
3   2   9
4   2   10



    a   b
0   1   6
1   1   7
3   2   9

Upvotes: 1

Views: 218

Answers (2)

BENY
BENY

Reputation: 323316

So duplicated

b[b.iloc[::-1].duplicated('a')]
   a  b
0  1  6
1  1  7
3  2  9

Upvotes: 3

rafaelc
rafaelc

Reputation: 59274

Use groupby+GroupBy.tail to get the index of the last row of each group, and drop them.

b.drop(b.groupby('a').tail(1).index)

   a  b
0  1  6
1  1  7
3  2  9

Upvotes: 2

Related Questions