Pandas Dataframe-将单列扩展为多列

时间:2019-06-04 01:39:59

标签: python pandas

我有一个2列的熊猫数据框social,如下所示:

 user_id    user_network
    1       [39, 77, 890, 1256, 1299, ...]
    2       [19, 66, 223, 567, 91008, ...]
    3       [69, 89, 92, 3478, 10529, ...]
    4       [25, 50, 556, 22904, 933875..]
    .       [...]
    .       [...]
    .       [...]

我想将第二列扩展为:

 user_id    friend_1    friend_2    friend_3   ...
    1          39          77          890     ...
    2          19          66          223     ...
    .          .           .           .       ...
    .          .           .           .       ...
    .          .           .           .       ...

我尝试了这个(假设第2列中的numpy数组的长度为20):

pandas.concat(social,
              pandas.Dataframe([n for row in social for n in row['user_network'],
                               social.index,
                               ["friend_{}".format(str(i+1) for i in len(20)]),
              axis=1).drop(column='user_network')

这不起作用,表示没有这样的row['user_network']。我想知道是否有API可以做这样的事情。我试图用谷歌搜索出一个智能解决方案,但一无所获。

0 个答案:

没有答案