0

I am trying to group by data into series. A similar question can be found on stackoverflow here but it groupby data into a list and I want into series. Any idea.

For list, the solution is like this

df1 = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6],'c':[1,2,5,5,4,6]})

df1.groupby('a').agg(list)

I tried something like this

df1.groupby(a).agg(lambda x: pd.Series(list(x)))

But its not working So I tried this

for i in df1.columns:
  df1[i]=df1[i].apply(lambda x:np.array(x))

But I want to convert to sereis

I want output like this

      b      c 
A   1 2     1 2
B   5 5 4   5 5 4
C   6       6
Talha Anwar
  • 2,699
  • 4
  • 23
  • 62

1 Answers1

0

Try:

df1.astype(str).groupby('a')['b'].agg(','.join).to_frame()

Output:

       b
a       
A    1,2
B  5,5,4
C      6
Scott Boston
  • 147,308
  • 15
  • 139
  • 187