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
643 views
in Technique[技术] by (71.8m points)

python - Stacking dataframe columns (Pandas)

I am looking for a way to pivot a dataframe in reverse direction. To the best of my knowledge, pandas provides a pivot or pivot_table method to transform an EAV df to a "normal" one. However, is there also a way to do the inverse?

So given the dataframe:

$df userid A B C 0 1 1 0 1 1 3 1 2 1 5 0

I would like to transform it to (a EAV model):

$df E A V
0 A 1 0 B 1 0 C 0 1 A 1 1 B 3 1 C 1 2 A 1 2 B 5 2 C 0

What would be the most efficient way to do so?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Assuming userid is the index, df.stack will do it:

In [133]: df.stack().reset_index().rename(columns={'userid' : 'E', 'level_1' : 'A', 0 : 'V'})
Out[133]: 
   E  A  V
0  0  A  1
1  0  B  1
2  0  C  0
3  1  A  1
4  1  B  3
5  1  C  1
6  2  A  1
7  2  B  5
8  2  C  0

If userid is not the index, set it like this:

df.set_index('userid', inplace=True)

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

...