[英]How to convert multiple columns in one column in pandas?
I have the following dataframe:我有以下 dataframe:
route1 route2
1 19.0 51.0
2 47.0 46.0
3 56.0 37.0
4 43.0 2.0
I would like to have the following output.我想要以下 output。
edge route
1 19.0 route1
2 47.0 route1
3 56.0 route1
4 43.0 route1
5 51.0 route2
6 46.0 route2
7 37.0 route2
8 2.0 route2
How can I do that?我怎样才能做到这一点? I checked that Pandas: Multiple columns into one column but it doesnt help that much with the new column
routes
that I want to create.我检查了Pandas: Multiple columns into one column但它对我要创建的新列
routes
没有太大帮助。
Thank you in advance!先感谢您!
>>> df.melt(var_name='route', value_name='edge')
route edge
0 route1 19.0
1 route1 47.0
2 route1 56.0
3 route1 43.0
4 route2 51.0
5 route2 46.0
6 route2 37.0
7 route2 2.0
If you have some columns to protect, use id_vars=['col1', 'col2', ...]
to not flatten them.如果您有一些列要保护,请使用
id_vars=['col1', 'col2', ...]
来不将它们展平。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.