Python 熊猫:如何使用列的其他行创建列表?
我有一个dataframe,其列如下所示:Python 熊猫:如何使用列的其他行创建列表?,python,pandas,Python,Pandas,我有一个dataframe,其列如下所示: Source 1 2 3 Source Related 1 [2,3] 2 [1,3] 3 [1,2] 我想做的是这样一个专栏: Source 1 2 3 Source Related 1 [2,3] 2 [1,3] 3 [1,2] 我怎样才能做到这一点?只需做一个“成本核算”的方法,但很容易理解 df['out'] = df.Source.apply(
Source
1
2
3
Source Related
1 [2,3]
2 [1,3]
3 [1,2]
我想做的是这样一个专栏:
Source
1
2
3
Source Related
1 [2,3]
2 [1,3]
3 [1,2]
我怎样才能做到这一点?只需做一个“成本核算”的方法,但很容易理解
df['out'] = df.Source.apply(lambda x : list(set(df.Source.tolist())-{x}))
Out[29]:
0 [2, 3]
1 [1, 3]
2 [1, 2]
Name: Source, dtype: object
只需采用“成本核算”的方式,但要易于理解
df['out'] = df.Source.apply(lambda x : list(set(df.Source.tolist())-{x}))
Out[29]:
0 [2, 3]
1 [1, 3]
2 [1, 2]
Name: Source, dtype: object
索引可能会有所帮助。您使用什么参数来为相关的。也许更多的信息可以帮助我们,索引可以帮助我们。您使用什么参数来为相关的。也许更多的信息可以帮助我们