gpt4 book ai didi

python - 如何通过文件处理从给定的数据框列中获取唯一对?

转载 作者:太空宇宙 更新时间:2023-11-03 19:47:10 24 4
gpt4 key购买 nike

来自数据帧的示例数据: sample data from dataframe

(8, 8), (8, 8), (8, 8), (8, 8), (8, 8)
(6, 7), (7, 7), (7, 7), (7, 6), (6, 7)
(2, 12), (12, 3), (3, 4), (4, 12), (12, 12)

```

new_col = []
for e in content.Pairs:
new_col.append(list(dict.fromkeys(e)))
content['Unique'] = new_col

```

预期输出是对列中的唯一对,如下所示:

(8, 8),(6, 7),(7, 6),(7, 7),(2, 12) so on

我在尝试上面的代码时得到的是这个结果:

独特

['8', '']
['6', '7', '']
['2', '12', '3', '4', '']

如果我使用手动数据,那么数据有什么问题,那么它可以工作,为什么不在数据框中呢

最佳答案

您可以使用set方法:

data = (((8, 8), (8, 8), (8, 8), (8, 8), (8, 8)),
((6, 7), (7, 7), (7, 7), (7, 6), (6, 7)),
((2, 12), (12, 3), (3, 4), (4, 12), (12, 12)))

uniques = []

for col in data:
for unique in list(set(col)):
uniques.append(unique)

for x in uniques:
print(x)

:

data = (((8, 8), (8, 8), (8, 8), (8, 8), (8, 8)),
((6, 7), (7, 7), (7, 7), (7, 6), (6, 7)),
((2, 12), (12, 3), (3, 4), (4, 12), (12, 12)))

uniques = []

for col in data:
uniques += [unique for unique in list(set(col))]

for x in uniques:
print(x)

关于python - 如何通过文件处理从给定的数据框列中获取唯一对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056340/

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