Skip to content Skip to sidebar Skip to footer

Python - Find Elements With Same Id In A Dataframe And Group Together

I have a data-frame like this : id value1 value2 0 1 1 1 2 3 2 1 4 3 1 5 4 2 1 i want it to be like this : id value1 value2 count

Solution 1:

Use aggregation by agg with join and size, but is necessary converting column to strings:

tups = [('value2', lambda x: ','.join(x.astype(str))), ('count', 'size')]
df1 = df.groupby('value1')['value2'].agg(tups).reset_index()
print (df1)
   value1 value2  count
011,4,53123,12

Alternative:

tups = [('value2', ','.join), ('count', 'size')]
df1 = df['value2'].astype(str).groupby(df['value1']).agg(tups).reset_index()

Solution 2:

Something like this could also work:

In [2468]: df.value2 = df['value2'].apply(str)

In [2494]: res = df.groupby('value1')['value2'].apply(lambda x:','.join(x)).reset_index()

In [2498]: res['count'] = df.groupby('value1').size().reset_index()[0]

In [2499]: res
Out[2499]: 
   value1 value2  count
011,4,53123,12

Post a Comment for "Python - Find Elements With Same Id In A Dataframe And Group Together"