Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...