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,750 points

edited Jun 8, 2020 by MD 1,305 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, 2020 by MD

Related Questions In Data Analytics

0 votes
0 answers

R function rep() in Python (replicates elements of a list/vector)

Each element of a vector is duplicated ...READ MORE

Jun 24, 2022 in Data Analytics by Sohail
• 3,040 points
1,332 views
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,660 points
1,169 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,130 points

edited Apr 12, 2018 by nirvana 22,096 views
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

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

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,042 views
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, 2020 by MD 2,049 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP