转置/将我的DF转换成新的形式。
DF =
Id music_item playlist_owner playlist entity_name
0 0 tune 6-9 my 14-15 elrow Guest List 17-32 O O
1 1 O O leticia 28-34 animal humor 36-47 157 Riverside Avenue 4-23转到
DF =
Id Goal Loc
0 0 music_item tune 6-9
1 0 playlist_owner my 14-15
2 0 playlist elrow Guest List 17-32
3 1 entity_name 157 Riverside Avenue 4-23
4 1 playlist_owner leticia 28-34
5 1 playlist animal humor 36-47有办法做到这一点吗?
发布于 2020-12-19 03:56:03
是。我真的不能复制你的数据帧,因为你张贴它的方式,但下面会得到你需要的。
假设您的数据帧名为df,您可以使用pd.melt()并重命名列:
new_df = pd.melt(df,id_vars='Id')
new_df.columns = ['Id','Goal','Loc']打印的内容:
new_df
Out[59]:
Id Goal Loc
0 0 music_item tune 6-9
1 1 music_item O O
2 0 playlist_owner my 14-15
3 1 playlist_owner leticia 28-34
4 0 playlist elrow Guest List 17-32
5 1 playlist animal humor 36-47
6 0 entity_name O O
7 1 entity_name Avenue 4-23https://stackoverflow.com/questions/65363104
复制相似问题