Skip to content Skip to sidebar Skip to footer

Pandas Melt Data With Many Id_vars Into On Column

I'd like to melt a DataFrame without using any loops. Suppose I have a DataFrame looking something like this: df = pd.DataFrame({'var1': [1,2,3,4,5]*100, 'var2':

Solution 1:

Use wide_to_long, it create new column by number after var, col columns, so if necessary added var strings:

df1 = (pd.wide_to_long(df.reset_index(), stubnames=['var','col'], i='index', j='new')
         .reset_index(level=1)
         .assign(new = lambda x: 'var' + x['new'].astype(str))
         .reset_index(drop=True)
         )
print (df1) 
      newvar col
0    var1    1   a
1    var1    2   b
2    var1    3   a
3    var1    4   b
4    var1    5   a
..    ...  ...  ..
995  var2    1   d
996  var2    2   c
997  var2    3   d
998  var2    4   c
999  var2    5   d

[1000 rows x 3 columns]

Possible a bit hack solution with melt:

df1 = df.melt(value_vars=['var1', 'var2'], var_name='var', id_vars=['col1', 'col2'])
df2 = df.melt(value_vars=['col1', 'col2'], var_name='var', id_vars=['var1', 'var2'])

df = pd.concat([df1[['var','value']], df2['value'].rename('col')], axis=1)
print (df)
      var  value col
0    var1      1   a
1    var1      2   b
2    var1      3   a
3    var1      4   b
4    var1      5   a
..    ...    ...  ..
995  var2      1   d
996  var2      2   c
997  var2      3   d
998  var2      4   c
999  var2      5   d

[1000 rows x 3 columns]

Post a Comment for "Pandas Melt Data With Many Id_vars Into On Column"