简体   繁体   中英

Grouping by unique values while transposing column

I asked a similar question the other day with data from two columns:

Grouping columns by unique values in Python

Now I have three columns. They need to be grouped by column A with column B as the header values and column C sorted properly.

My data frame looks like:

    A   B   C
25115  20  45
25115  30  154
25115  40  87
25115  70  21
25115  90  74
26200  10  48
26200  20  414
26200  40  21
26200  50  288
26200  80  174
26200  90  54

But I need to end up with this:

       10   20   30   40   50   70   80   90
25115       45   154  87        21        74
26200  48   414       21   288       174  54

This gets the values of column C, but not with column B as the row names.

import pandas as pd
df = pd.DataFrame({'A':[25115,25115,25115,25115,25115,26200,26200,26200,26200,26200,26200],'B':[20,30,40,70,90,10,20,40,50,80,90],'C':[45,154,87,21,74,48,414,21,288,174,54]})
a = df.groupby('A')['C'].apply(lambda x:' '.join(x.astype(str)))

Any ideas would be most appreciated.

  • Option 1:

Use pivot_table:

df.pivot_table(values='C',index='A',columns='B')

Output

B        10     20     30    40     50    70     80    90
A                                                        
25115   NaN   45.0  154.0  87.0    NaN  21.0    NaN  74.0
26200  48.0  414.0    NaN  21.0  288.0   NaN  174.0  54.0
  • Option 2:

Use set_index / unstack:

df.set_index(['A','B'])['C'].unstack()

Output:

B        10     20     30    40     50    70     80    90
A                                                        
25115   NaN   45.0  154.0  87.0    NaN  21.0    NaN  74.0
26200  48.0  414.0    NaN  21.0  288.0   NaN  174.0  54.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM