根据唯一 ID 将 pandas 数据框行转换为列

Turn pandas dataframe row in to a column based on unique IDs

我有一个类似于下面的数据框。 'song obj'实际上是关于一首歌的各种信息的嵌套字典,例如

{data:{album:{album name:---, album id:---}},track{track name:---,track id:---}}

有些播放列表很长,因此有多达 400 列,每列都标有一个整数。

df = pd.DataFrame({"playlist id" : {0 : 8179, 1:8764, 2:8776},
                0 : {0:"song obj",1:"song obj",2:"song obj"},
                1: {0:"song obj",1:"song obj",2:"song obj"},
                2 : {0:"song obj",1:"song obj",2:"song obj"}   
               })

看起来像这样:

>>>
    playlist  id rating 0           1           2
0   8179      3         song obj 1  song obj 2  song obj 3
1   8764      2         song obj 4  song obj 5  NaN
2   8776      4         song obj 6  song obj 7  song obj 8

我试图让上面的数据框看起来像这个,所有 'song objects' 垂直但在它们相应的播放列表编号旁边,如下面的数据框:

df1=pd.DataFrame({
"playlist id": {0: 8179,1:8179,2:8179,3:8764,4:8764,
                5:8776,6:8776,7:8776},
"rating": {0:3, 1:3, 2:3, 3:2, 4:2, 5:4, 6:4, 7:4},
"song objects": {0:"song obj 1", 1:"song obj 2", 2:"song obj 3", 
                 3:"song obj 4", 4:"song obj 5",5:"song obj 6",
                 6:"song obj 7",7:"song obj 8"}
             })
>>>
    playlist id  rating song objects
  0 8179         3      song obj 1
  1 8179         3      song obj 2
  2 8179         3      song obj 3
  3 8764         2      song obj 4
  4 8764         2      song obj 5
  5 8776         4      song obj 6
  6 8776         4      song obj 7
  7 8776         4      song obj 8

我试过使用 pd.wide_to_longpd.melt 但到目前为止我没有运气。我也尝试过在堆栈交换上搜索,但是因为我是 programming/python/pandas 的新手,所以我没有正确的术语来找到我需要的东西..请帮忙!

注意:原因是因为最终我想'unpack'把'song object'放到新的栏目中。我认为

可以做到这一点
df2=pd.concat([df2.drop(['song objects'], axis=1), df2['song objects'].apply(pd.Series)], axis=1)

您必须稍微修改一下,因为您的示例代码不会产生您指定的输出,例如 rating 列。

import pandas as pd

df = pd.DataFrame({"playlist id" : {0 : 8179, 1:8764, 2:8776},
                0 : {0:"song obj",1:"song obj",2:"song obj"},
                1: {0:"song obj",1:"song obj",2:"song obj"},
                2 : {0:"song obj",1:"song obj",2:"song obj"}   
               })

pd.melt(df, id_vars = ['playlist id'], value_vars = [0, 1, 2])

输出:

    playlist id     variable    value
0   8179    0   song obj
1   8764    0   song obj
2   8776    0   song obj
3   8179    1   song obj
4   8764    1   song obj
5   8776    1   song obj
6   8179    2   song obj
7   8764    2   song obj
8   8776    2   song obj