首页 > 解决方案 > 创建一列边

问题描述

我需要将列中的 uid 连接到uids列列表中的每个 uid,friends如下例所示:

给定一个pandas.DataFrame对象A

    uid friends
0   1   [10, 2, 1, 5]
1   2   [1, 2]
2   3   [5, 4]
3   4   [10, 5]
4   5   [1, 2, 5]

所需的输出是:

    uid friends         in_edges
0   1   [10, 2, 1, 5]   [(1, 10), (1, 2), (1, 1), (1, 5)]
1   2   [1, 2]          [(2, 1), (2, 2)]
2   3   [5, 4]          [(3, 5), (3, 4)]
3   4   [10, 5]         [(4, 10), (4, 5)]
4   5   [1, 2, 5]       [(5, 1), (5, 2), (5, 5)]

我使用以下代码来实现此结果:

import numpy as np
import pandas as pd

A = pd.DataFrame(dict(uid=[1, 2, 3, 4, 5], friends=[[10, 2, 1, 5], [1, 2], [5, 4], [10, 5], [1, 2, 5]]))

A.loc[:, 'in_edges'] = A.loc[:, 'uid'].apply(lambda uid: [(uid, f) for f in A.loc[A.loc[:, 'uid']==uid, 'friends'].values[0]])

但这A.loc[A.loc[:, 'uid']==uid, 'friends']部分对我来说看起来有点麻烦,所以我想知道是否有更简单的方法来完成这项任务?

提前致谢。

标签: python-3.xpandasdataframe

解决方案


您可以使用.apply()withaxis=1参数:

df["in_edges"] = df[["uid", "friends"]].apply(
    lambda x: [(x["uid"], f) for f in x["friends"]], axis=1
)
print(df)

印刷:

   uid        friends                           in_edges
0    1  [10, 2, 1, 5]  [(1, 10), (1, 2), (1, 1), (1, 5)]
1    2         [1, 2]                   [(2, 1), (2, 2)]
2    3         [5, 4]                   [(3, 5), (3, 4)]
3    4        [10, 5]                  [(4, 10), (4, 5)]
4    5      [1, 2, 5]           [(5, 1), (5, 2), (5, 5)]

推荐阅读