0

Here is an toy-example of transforming df into df_f (basically it is kind of wide to long transform).

I wonder if it could be done in more cleaner, concise and direct way?

import pandas as pd

df = pd.DataFrame({'x': ['a', 'b', 'c'], 'A': [1,2,3], 'B':[7,8,9]})

     x  A   B
 0   a  1   7
 1   b  2   8
 2   c  3   9

cols_to_stack = ['A', 'B']
df_s = df[cols_to_stack].stack().rename_axis(('idx', 'c')).reset_index(name='value')

    idx c   value
0   0   A   1
1   0   B   7
2   1   A   2
3   1   B   8
4   2   A   3
5   2   B   9

df_ind = df[['x']] #there could be more columns than x, but we can assume that will be df.columns "minus" col_to_stack

df_f = pd.merge(df_s, df_ind, how='left', left_on='idx', right_index=True).drop(columns=['idx'])

    c value x
0   A   1   a
1   B   7   a
2   A   2   b
3   B   8   b
4   A   3   c
5   B   9   c
Quant Christo
  • 1,275
  • 9
  • 23

0 Answers0