gpt4 book ai didi

python - 如何从列中选择所有元素,它是python中DataFrame中的列表?

转载 作者:行者123 更新时间:2023-12-04 08:41:48 28 4
gpt4 key购买 nike

我正在使用字典,该字典的键值由列表列表组成。我将其转换为DataFrame,输出如下:

nodes_labels = DataFrame.from_dict(nodes_neighbors, orient='index')
nodes_labels.columns = ['Neighbor','Degree', 'max_Similar', 'NI_Label']


Neighbor ... NI_Label
0 [1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 17, 1... ... [2.203889356257777, 0, 1]
3 [0, 1, 2, 7, 12, 13] ... [2.0422979797979797, 0, 1]
33 [8, 9, 13, 14, 15, 18, 19, 20, 22, 23, 26, 27,... ... [1.8302700210614677, 32, 0]
1 [0, 2, 3, 7, 13, 17, 19, 21, 30] ... [1.8283661952083003, 0, 1]
32 [2, 8, 14, 15, 18, 20, 22, 23, 29, 30, 31, 33] ... [1.74376221404619, 32, 0]
7 [0, 1, 2, 3] ... [1.3425824175824175, 0, 1]
2 [0, 1, 3, 7, 8, 9, 13, 27, 28, 32] ... [1.2265813134234187, 0, 1]
13 [0, 1, 2, 3, 33] ... [0.8298701298701299, 0, 1]
29 [23, 26, 32, 33] ... [0.5291005291005291, 32, 0]

我想选择列 NI_Label并从中我想为所有行选择列表中的第二个元素。我想要的结果如下:
       NI_Label
0 0
3 0
33 32
1 0
32 32
7 0
2 0
13 0
29 32
我只能选择列 NI_Label但我不知道如何为所有行选择列表中的第二个元素

最佳答案

您可以 .apply使用自定义 lambda 函数:

print(nodes_labels['NI_Label'].apply(lambda x: x[1]))

关于python - 如何从列中选择所有元素,它是python中DataFrame中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64528661/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com