Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform rows into columns for my case?

Tags:

python

pandas

I have the following pandas DataFrame df:

SIGN        TYPE    TIME                         ADDITIONAL
ABC5245     10      2017-01-01 01:52:25.000      2017-01-01 01:39:04.000
ABC5245     20      2017-01-01 01:53:22.000      2017-01-01 02:39:04.000
DEF1111     20      2017-01-01 01:57:00.000      2017-01-01 03:39:04.000
DEF1111     10      2017-01-01 01:55:15.000      2017-01-01 01:39:04.000
AAA2222     10      2017-01-01 01:57:00.000      2017-01-01 01:39:04.000

I need to group data by SIGN and create four new columns based on TYPE: TIME_10, TIME_20, ADDITIONAL_10 and ADDITIONAL_20.

This is the expected result:

SIGN        TIME_10                  TIME_20                  ADDITIONAL_10             ADDITIONAL_20
ABC5245     2017-01-01 01:52:25.000  2017-01-01 01:53:22.000  2017-01-01 01:39:04.000   2017-01-01 02:39:04.000         
DEF1111     2017-01-01 01:55:15.000  2017-01-01 01:57:00.000  2017-01-01 01:39:04.000   2017-01-01 03:39:04.000
AAA2222     2017-01-01 01:57:00.000  NaN                      2017-01-01 01:39:04.000   NaN
like image 345
ScalaBoy Avatar asked Mar 04 '23 15:03

ScalaBoy


2 Answers

Use reshaping and flattening column header multiindex

df_out = df.set_index(['SIGN','TYPE']).unstack('TYPE')
df_out.columns = [f'{i}_{j}' for i, j in df_out.columns]
print(df_out)

Output:

                         TIME_10                  TIME_20  \
SIGN                                                        
AAA2222  2017-01-01 01:57:00.000                      NaN   
ABC5245  2017-01-01 01:52:25.000  2017-01-01 01:53:22.000   
DEF1111  2017-01-01 01:55:15.000  2017-01-01 01:57:00.000   

                   ADDITIONAL_10            ADDITIONAL_20  
SIGN                                                       
AAA2222  2017-01-01 01:39:04.000                      NaN  
ABC5245  2017-01-01 01:39:04.000  2017-01-01 02:39:04.000  
DEF1111  2017-01-01 01:39:04.000  2017-01-01 03:39:04.000  
like image 54
Scott Boston Avatar answered Mar 16 '23 04:03

Scott Boston


You can get the result with a pivot. If you are okay with the columns being a MultiIndex, the second line is not needed.

Thanks @ScottBoston for the tip on column formatting.

df = df.pivot('SIGN', 'TYPE', ['TIME', 'ADDITIONAL'])                          
df.columns = df.columns.map('{0[0]}_{0[1]}'.format)

EDIT

In context:

import pandas as pd
data = [
    ['ABC5245', 10, '2017-01-01 01:52:25.000', '2017-01-01 01:39:04.000'],
    ['ABC5245', 20, '2017-01-01 01:53:22.000', '2017-01-01 02:39:04.000'],
    ['DEF1111', 20, '2017-01-01 01:57:00.000', '2017-01-01 03:39:04.000'],
    ['DEF1111', 10, '2017-01-01 01:55:15.000', '2017-01-01 01:39:04.000'],
    ['AAA2222', 10, '2017-01-01 01:57:00.000', '2017-01-01 01:39:04.000'],
]
columns = ['SIGN', 'TYPE', 'TIME', 'ADDITIONAL']

df = pd.DataFrame(data=data, columns=columns)
print(df)

df = df.pivot('SIGN', 'TYPE', ['TIME', 'ADDITIONAL'])
df.columns = df.columns.map('{0[0]}_{0[1]}'.format)
print(df)

Output:

      SIGN  TYPE                     TIME               ADDITIONAL
0  ABC5245    10  2017-01-01 01:52:25.000  2017-01-01 01:39:04.000
1  ABC5245    20  2017-01-01 01:53:22.000  2017-01-01 02:39:04.000
2  DEF1111    20  2017-01-01 01:57:00.000  2017-01-01 03:39:04.000
3  DEF1111    10  2017-01-01 01:55:15.000  2017-01-01 01:39:04.000
4  AAA2222    10  2017-01-01 01:57:00.000  2017-01-01 01:39:04.000
                         TIME_10                  TIME_20            ADDITIONAL_10            ADDITIONAL_20
SIGN
AAA2222  2017-01-01 01:57:00.000                      NaN  2017-01-01 01:39:04.000                      NaN
ABC5245  2017-01-01 01:52:25.000  2017-01-01 01:53:22.000  2017-01-01 01:39:04.000  2017-01-01 02:39:04.000
DEF1111  2017-01-01 01:55:15.000  2017-01-01 01:57:00.000  2017-01-01 01:39:04.000  2017-01-01 03:39:04.000
like image 31
brentertainer Avatar answered Mar 16 '23 04:03

brentertainer