-1

new in pandas and I was able to create a dataframe from a csv file. I was also able to sort it out.

What I am struggling now is the following: I give an image as an example from a pandas data frame. A data frame

First column is the index, Second column is a group number Third column is what happened.

I want based on the second column to take out the third column on the same unique data frame.

I highlight few examples: For the number 9 return back the sequence [60,61,70,51]

For the number 6 get back the sequence [65,55,56]

For the number 8 get back the single element 8.

How groupby can be used to do this extraction?

Thanks a lot Regards Alex

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alex P
  • 137
  • 1
  • 13
  • 6
    Please provide actual data we can easily copy&paste instead of an image. Then it becomes easier to help. – Cleb Jun 04 '19 at 09:30
  • As already said [don't post pictures of code/data](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Plus do you only want to get those exact groups? So `9,6, and 8` or all groups? – Erfan Jun 04 '19 at 09:31
  • And it would help to have the columns names... – Serge Ballesta Jun 04 '19 at 09:42
  • thanks. I want to extract the values. My wording about unique was on storing all those vectors on the same data frame. – Alex P Jun 04 '19 at 10:31

1 Answers1

1

Starting from the answers on this question we can extract following code to receive the desired result.

dataframe =  pd.DataFrame({'index':[0,1,2,3,4], 'groupNumber':[9,9,9,9,9], 'value':[12,13,14,15,16]})
grouped = dataframe.groupby('groupNumber')['value'].apply(list)
SBylemans
  • 1,764
  • 13
  • 28
  • yes my question was answered there. My solutions is to apply a list after the group by df_sorted_unique.groupby('A')['B'].apply(list). – Alex P Jun 04 '19 at 10:41