Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Dataframe groupby Display

Tags:

python

pandas

Lets say I have a DataFrame like following.

      A      B
0    text1   200
1    text2   200
2    text1   400
3    text2   500
4    text1   300
5    text1   600
6    text2   300

I want to print following output

     A             B
0    text1,text2   200
2    text1         400
3    text2         500
4    text1,text2   300
5    text1         600

There is no order,I just want to take text labels of column "A" for matching values in column "B". This has to be done with df.groupby as I know. Any way no success with my efforts yet. Hope you get my question.

like image 380
Nilani Algiriyage Avatar asked Aug 05 '13 10:08

Nilani Algiriyage


People also ask

How do you get Groupby rows in pandas?

You can group DataFrame rows into a list by using pandas. DataFrame. groupby() function on the column of interest, select the column you want as a list from group and then use Series. apply(list) to get the list for every group.

Can you use Groupby on a DataFrame?

groupby() function on any categorical column of DataFrame, it returns a GroupBy object. Then you can use different methods on this object and even aggregate other columns to get the summary view of the dataset.


1 Answers

You can nearly do this with a plain ol' sum:

In [11]: df.groupby('B').sum()
Out[11]:
              A
B
200  text1text2
300  text1text2
400       text1
500       text2
600       text1

You could use an aggregate with a join:

In [12]: df.groupby('B').agg(lambda x: ', '.join(x.values))
Out[12]:
                A
B
200  text1, text2
300  text1, text2
400         text1
500         text2
600         text1
like image 189
Andy Hayden Avatar answered Nov 05 '22 19:11

Andy Hayden