简体   繁体   中英

How to repeat a row for each value of its corresponding columns items

col1 col2 col3
(str)1 (str)3 (str)34
(str)2 (str)4 (str)2

to

col1 col2
(str)1 (str)3
(str)1 (str)34
(str)2 (str)4
(str)2 (str)2

Try with melt then sort_values

out = df.melt('col1').sort_values('col1')#.drop('variable',1)
Out[426]: 
     col1 variable    value
0  (str)1     col2   (str)3
2  (str)1     col3  (str)34
1  (str)2     col2   (str)4
3  (str)2     col3   (str)2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM