Python 从包含整数列表的列中获取唯一的组合
我有一个熊猫专栏Python 从包含整数列表的列中获取唯一的组合,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个熊猫专栏 [1, 1539, 21] [1, 636, 83] [1, 636, 84] 重新创建列的代码 x = pd.DataFrame({ 'array' : [[1, 1539, 21],[1, 636, 83],[1, 636, 84]] }) 如果我们求解值1 反向连接=[](空,因为在每一行中,1没有反向连接) forward_connections=[1539636](有两个636连接,但由于我们正在查找唯一的连接,因此将被计为一个) 作为输出,我想列出每个值
[1, 1539, 21]
[1, 636, 83]
[1, 636, 84]
重新创建列的代码
x = pd.DataFrame({
'array' : [[1, 1539, 21],[1, 636, 83],[1, 636, 84]]
})
如果我们求解值1
反向连接=[](空,因为在每一行中,1没有反向连接)
forward_connections=[1539636](有两个636连接,但由于我们正在查找唯一的连接,因此将被计为一个)
作为输出,我想列出每个值的唯一向后和向前连接
以下是完整的解决方案
Value backward_connections forward_connections unique_connections
0 1 [] [1539, 636] 2
1 21 [1593] [] 1
2 83 [636] [] 1
3 84 [636] [] 1
4 636 [1] [83,84] 3
5 1539 [] [21] 2
这有点有趣
# create a set of all unique values in df
unique_values = set([v for t in x['array'].tolist() for v in t])
# create a default dictionary from these values
result_dic = {value : {'previous': [], 'forward': []} for value in unique_values}
for value in unique_values:
for list_ in x['array']:
if value in list_:
# get the value's index in the list
value_index = list_.index(value)
# some logic for previous
if value_index != 0:
result_dic[value]['previous'].append(list_[value_index - 1])
# some logic for forward
if value_index != len(list_)-1:
result_dic[value]['forward'].append(list_[value_index + 1])
# back to a df
result_df = pandas.DataFrame.from_dict(result_dic, orient='index').reset_index()
# removing duplicate values in the lists
result_df[['previous', 'forward']] = result_df[['previous', 'forward']].applymap(lambda x: list(set(x)))
# counting unique connections
result_df['unique_connections'] = result_df['previous'].map(len) + result_df['forward'].map(len)
result_df
产出
index previous forward unique_connections
0 1 [] [1539, 636] 2
1 1539 [1] [21] 2
2 83 [636] [] 1
3 84 [636] [] 1
4 21 [1539] [] 1
5 636 [1] [83, 84] 3
请共享数据,而不是图片。@sammywemmy已添加。
x['array'].unique()
?或者value\u counts()
请添加所需的规则,以获得所需的输出格式。您没有提供任何关于上一个和下一个是什么的详细信息。如果我们只关心值对,为什么原始数据帧中有三列?