繁体   English   中英

如何为其对应列项的每个值重复一行

[英]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

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

尝试使用melt然后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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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