How to get the unique pairs from the given data frame column with file handling?

与世无争的帅哥 提交于 2020-02-08 02:31:13

问题


sample data from dataframe:

Pairs

(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

    ```

output expected is unique pairs from Pair column like this:

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

what I am getting is this result when trying the above code:

Unique

['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


回答1:


You could use the set method:

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)



回答2:


You can use set() method to remove duplicates from the list of tuples.

 >>> items = [(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)]
>>> set_items = set(items)
>>> set_items
{(6, 7), (7, 6), (12, 12), (7, 7), (8, 8), (4, 12), (2, 12), (3, 4), (12, 3)}


来源:https://stackoverflow.com/questions/60056339/how-to-get-the-unique-pairs-from-the-given-data-frame-column-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!