简体   繁体   中英

Python Dataframe: Create columns based on another column

I have a dataframe with repeated values for one column (here column 'A') and I want to convert this dataframe so that new columns are formed based on values of column 'A'.

Example

df = pd.DataFrame({'A':range(4)*3, 'B':range(12),'C':range(12,24)})
df
    A   B   C
0   0   0  12
1   1   1  13
2   2   2  14
3   3   3  15
4   0   4  16
5   1   5  17
6   2   6  18
7   3   7  19
8   0   8  20
9   1   9  21
10  2  10  22
11  3  11  23

Note that the values of "A" column are repeated 3 times.

Now I want the simplest solution to convert it to another dataframe with this configuration (please ignore the naming of the columns, it is used for description purpose only, they could be anything):

        B               C
    A0  A1  A2  A3  A0  A1  A2  A3
0   0   1   2   3   12  13  14  15
1   4   5   6   7   16  17  18  19
2   8   9   10  11  20  21  22  23

You may need assign the group help key by cumcount , then just do unstack

yourdf=df.assign(D=df.groupby('A').cumcount(),A='A'+df.A.astype(str)).set_index(['D','A']).unstack()
   B              C            
A A0 A1  A2  A3  A0  A1  A2  A3
D                              
0  0  1   2   3  12  13  14  15
1  4  5   6   7  16  17  18  19
2  8  9  10  11  20  21  22  23

This is a pivot problem, so use

df.assign(idx=df.groupby('A').cumcount()).pivot('idx', 'A', ['B', 'C'])

     B              C            
A    0  1   2   3   0   1   2   3
idx                              
0    0  1   2   3  12  13  14  15
1    4  5   6   7  16  17  18  19
2    8  9  10  11  20  21  22  23

If the headers are important, you can use MultiIndex.set_levels to fix them.

u = df.assign(idx=df.groupby('A').cumcount()).pivot('idx', 'A', ['B', 'C'])
u.columns = u.columns.set_levels(
    ['A' + u.columns.levels[1].astype(str)], level=[1])
u

     B              C            
A   A0 A1  A2  A3  A0  A1  A2  A3
idx                              
0    0  1   2   3  12  13  14  15
1    4  5   6   7  16  17  18  19
2    8  9  10  11  20  21  22  23

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