简体   繁体   中英

Reshape dataframe in Pandas from long to wide format with new column names

I have a dataframe that I need reshaped (sample below). I want only one unique user per row, but, right now, each user has two rows in the dataframe with different values based on the 'testday' column (baseline and D7). What I'd like is to rename the value columns ('01. Tristeza Aparente) based on the column name of the testday groups. So, the new value columns would be something like 'Basel_Tristeza Aparente' and 'D7_01. Tristeza Aparente'

The tutorials I've ready on Pivot and unstack don't quite work, because I'm not trying to aggregate the data. I just need different columns when collapsing users into a single row. Thanks and please let me know if I can make this question more clear

  {'01. Tristeza Aparente': {0: 4.0,
  1: 4.0,
  2: 4.0,
  3: 2.0,
  4: 1.0,
  5: 0.0,
  6: 3.0},
 '02. Tristeza Expressa': {0: 6.0,
  1: 6.0,
  2: 4.0,
  3: 0.0,
  4: 4.0,
  5: 3.0,
  6: 6.0},
 'group': {0: 'placebo',
  1: 'placebo',
  2: 'placebo',
  3: 'placebo',
  4: 'placebo',
  5: 'placebo',
  6: 'placebo'},
 'subject': {0: 1.0, 1: nan, 2: 2.0, 3: nan, 4: 3.0, 5: nan, 6: 4.0},
 'subjectedit': {0: 1.0, 1: 1.0, 2: 2.0, 3: 2.0, 4: 3.0, 5: 3.0, 6: 4.0},
 'testday': {0: 'Basal',
  1: 'D7',
  2: 'Basal',
  3: 'D7',
  4: 'Basal',
  5: 'D7',
  6: 'Basal'}}

Is this df['new_column'] = df['testday'] + '_' + '01. Tristeza Aparente' df['new_column'] = df['testday'] + '_' + '01. Tristeza Aparente' going to solve your issue? you can also assign it to an existing column.

You can pivot the dataframe and rename columns with a formatted string with f , but make sure you are on the latest version of pandas, as pivot is buggy with earlier versions.

df = df.pivot(index=['group', 'subjectedit'], columns='testday')
df.columns = [f'{col[1]}_{col[0]}' for col in df.columns]
df
Out[1]: 
                     Basal_01. Tristeza Aparente  D7_01. Tristeza Aparente  \
group   subjectedit                                                          
placebo 1.0                                  4.0                       4.0   
        2.0                                  4.0                       2.0   
        3.0                                  1.0                       0.0   
        4.0                                  3.0                       NaN   

                     Basal_02. Tristeza Expressa  D7_02. Tristeza Expressa  \
group   subjectedit                                                          
placebo 1.0                                  6.0                       6.0   
        2.0                                  4.0                       0.0   
        3.0                                  4.0                       3.0   
        4.0                                  6.0                       NaN   

                     Basal_subject  D7_subject  
group   subjectedit                             
placebo 1.0                    1.0         NaN  
        2.0                    2.0         NaN  
        3.0                    3.0         NaN  
        4.0                    4.0         NaN  

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