asegovianot
asegovianot

Reputation: 13

groupby column in pandas

I am trying to groupby columns value in pandas but I'm not getting.

Example:

Col1     Col2     Col3
A        1        2
B        5        6
A        3        4 
C        7        8
A        11       12
B        9        10
-----

result needed grouping by Col1

Col1     Col2     Col3  
A        1,3,11   2,4,12
B        5,9      6,10
c        7        8

but I getting this ouput

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x0000025BEB4D6E50>

I am getting using excel power query with function group by and count all rows, but I can´t get the same with python and pandas. Any help?

Upvotes: 0

Views: 84

Answers (2)

Edmundas U.
Edmundas U.

Reputation: 1

Very good I created solution between 0 and 0:

df[df['A'] != 0].groupby((df['A'] == 0).cumsum()).sub()

It will group column between 0 and 0 and sum it

Upvotes: 0

Daniel R
Daniel R

Reputation: 2042

Try this

(
    df
    .groupby('Col1')
    .agg(lambda x: ','.join(x.astype(str)))
    .reset_index()
)

it outputs

  Col1    Col2    Col3
0    A  1,3,11  2,4,12
1    B     5,9    6,10
2    C       7       8

Upvotes: 1

Related Questions