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

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

本文介绍了如何通过文件处理从给定的数据帧列中获取唯一对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自数据框的样本数据:

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', '']

如果我处理的是手动数据,那么数据有什么问题,那为什么行得通,为什么不在数据框中

what is the issue with the data if I am doing with manual data then it's working why not in the data frame

推荐答案

您可以使用 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)

OR :

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)

这篇关于如何通过文件处理从给定的数据帧列中获取唯一对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:36