Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - pandas dataframe reshaping/stacking of multiple value variables into seperate columns

Hi I'm trying to reshape a data frame in a certain way.

this is the data frame I have,

         des1 des2 des3 interval1 interval2 interval3
value   
aaa       a    b    c     ##1         ##2       ##3
bbb       d    e    f     ##4         ##5       ##6
ccc       g    h    i     ##7         ##8       ##9

des1 corresponds with interval1 and so on. interval columns have a range of dates and des columns have descriptions.

I'd like to reshape the dataframe such that it looks like this:

         des      interval
value   
aaa       a         ##1
aaa       b         ##2
aaa       c         ##3
bbb       d         ##4
bbb       e         ##5
bbb       f         ##6
ccc       g         ##7
ccc       h         ##8
ccc       i         ##9

How would I go about doing this? I'm a little familar with .stack() but I haven't been able to get exactly what I wanted.

Thank you for your help. feel free to post references.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This might be a shorter approach:

[72]:

df.columns = pd.MultiIndex.from_tuples(map(lambda x: (x[:-1], x), df.columns))
In [73]:

print pd.DataFrame({key:df[key].stack().values for key in set(df.columns.get_level_values(0))},
                   index = df['des'].stack().index.get_level_values(0))
      des interval
value             
aaa     a      ##1
aaa     b      ##2
aaa     c      ##3
bbb     d      ##4
bbb     e      ##5
bbb     f      ##6
ccc     g      ##7
ccc     h      ##8
ccc     i      ##9

Or preserve the 1,2,3 info:

[73]:

df.columns = pd.MultiIndex.from_tuples(map(lambda x: (x[:-1], x[-1]), df.columns))
Keys = set(df.columns.get_level_values(0))
df2  = pd.concat([df[key].stack() for key in Keys], axis=1)
df2.columns = Keys
print df2
        des interval
value               
aaa   1   a      ##1
      2   b      ##2
      3   c      ##3
bbb   1   d      ##4
      2   e      ##5
      3   f      ##6
ccc   1   g      ##7
      2   h      ##8
      3   i      ##9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...