Python pandas - grouping and field summary

I recently played with Panda DataFrames and struggled to parse some multidimensional data.

Let's say I have some data, such as below:

order | sample | feature1 | feature2
-------------------------------------
1234  | A      | 0.20     | 0.45
1234  | B      | 0.71     | 0.08
1234  | C      | 0.21     | 0.02
1234  | D      | 0.87     | 0.88
5678  | A      | 0.76     | 0.42
5678  | B      | 0.01     | 0.03
5678  | C      | 0.29     | 0.91
5678  | D      | 0.70     | 0.78

And I want everything in the output to be grouped in order and where each function is summed using a sample:

order | feature1                  | feature2 
      | A    | B    | C    | D    | A    | B    | C    | D   
------------------------------------------------------------
1234  | 0.20 | 0.71 | 0.21 | 0.87 | 0.45 | 0.08 | 0.02 | 0.88
5678  | 0.76 | 0.01 | 0.29 | 0.70 | 0.42 | 0.03 | 0.91 | 0.78

Here is what I still have:

from pandas import *
df = DataFrame({"order": [1234, 1234, 1234, 1234, 5678, 5678, 5678, 5678], "sample": ["A", "B", "C", "D", "A", "B", "C", "D"], "feature1": [0.20, 0.71, 0.21, 0.87, 0.76, 0.01, 0.29, 0.70], "feature2": [0.45, 0.08, 0.02, 0.88, 0.42, 0.03, 0.91, 0.78]})
byorder = df.groupby("order")
# not sure how to go from 1 groupby object to a new dataframe having what i need

Do you have any thoughts on how I can get a new DataFrame containing aggregated data as I need? Maybe DataFrames are not suitable for this kind of manipulation?

+4
source share
1 answer

you can use pivot

>>> df.pivot(index='order', columns='sample')
       feature1                   feature2
sample        A     B     C     D        A     B     C     D
order
1234       0.20  0.71  0.21  0.87     0.45  0.08  0.02  0.88
5678       0.76  0.01  0.29  0.70     0.42  0.03  0.91  0.78
+6
source

Source: https://habr.com/ru/post/1589516/


All Articles