Grouping rows in a list in pandas (python) groupby

0 votes

I have a DataFrame:

a b
A 1
A 2
B 5
B 5
B 4
C 6

I want to group the first element and get the second element as list rows, is it possible?

May 3, 2018 in Data Analytics by CodingByHeart77
• 3,710 points

edited Jun 8 by MD 545 views

1 answer to this question.

0 votes

Yes, this possible by using groupby function to group on the column of your choice and then apply the list to every group: 

df.groupby('a')['b'].apply(list)  

The output you will get as given-below.

a 
A [1, 2] 
B [5, 5, 4] 
C [6]
answered May 3, 2018 by DeepCoder786
• 1,720 points

edited Jun 8 by MD

Related Questions In Data Analytics

0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,560 points
186 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,060 points

edited Apr 12, 2018 by nirvana 12,336 views
0 votes
1 answer
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 32,460 points
10,801 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,190 views
0 votes
0 answers
+4 votes
6 answers
0 votes
7 answers
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8 by MD 400 views