Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert header into row

I have a table like this.

user    01/12/15    02/12/15 someBool
u1      100         300      true
u2      200        -100      false
u3     -50          200      true

I want to repartition the date columns into two columns date and value like this.

user    date       value   someBool
u1      01/12/15   100     true
u1      02/12/15   300     true
u2      01/12/15   200     false
u2      02/12/15  -100     false
u3      01/12/15   50      true
u3      02/12/15   200     true

How to do this in python ? Is pivot_table in pandas helpful?

If possible provide code/psuedo code & give details on python version.

like image 550
Deepak Punjabi Avatar asked Dec 24 '22 21:12

Deepak Punjabi


2 Answers

You need melt:

df = pd.melt(df, id_vars=['user','someBool'], var_name='date')
print (df)
  user someBool      date  value
0   u1     True  01/12/15    100
1   u2    False  01/12/15    200
2   u3     True  01/12/15    -50
3   u1     True  02/12/15    300
4   u2    False  02/12/15   -100
5   u3     True  02/12/15    200

Another solution with stack:

df = df.set_index(['user','someBool'])
       .stack()
       .reset_index(name='value')
       .rename(columns={'level_2':'date'})
print (df)
  user someBool      date  value
0   u1     True  01/12/15    100
1   u1     True  02/12/15    300
2   u2    False  01/12/15    200
3   u2    False  02/12/15   -100
4   u3     True  01/12/15    -50
5   u3     True  02/12/15    200
like image 109
jezrael Avatar answered Dec 26 '22 19:12

jezrael


numpy reconstruct the whole thing

id_vars = ['user', 'someBool']

the_rest = df.columns.difference(id_vars).tolist()
m, n = len(df), len(the_rest)
var_slc = np.arange(m).repeat(n)

pd.DataFrame(
    np.hstack([
            df[id_vars].values[var_slc],
            np.tile(the_rest, m)[:, None],
            df[the_rest].values.reshape(-1, 1)
        ]), columns=id_vars + ['date', 'value']
)

  user someBool      date value
0   u1     True  01/12/15   100
1   u1     True  02/12/15   300
2   u2    False  01/12/15   200
3   u2    False  02/12/15  -100
4   u3     True  01/12/15   -50
5   u3     True  02/12/15   200
like image 22
piRSquared Avatar answered Dec 26 '22 18:12

piRSquared