简体   繁体   English

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

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

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:预期输出是来自 Pair 列的唯一对,如下所示:

(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如果我使用手动数据,那么数据有什么问题,为什么不在数据框中工作

You could use the set method:您可以使用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)

You can use set() method to remove duplicates from the list of tuples.您可以使用 set() 方法从元组列表中删除重复项。

 >>> 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)}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定另一列中的约束,如何从数据框创建对? - How do I create pairs from a data frame given constraints found in another column? 如何使用python中的文件处理从字符串数据的列中获取唯一值 - How to get the unique values from column on string data with file handling in python 如何有效地从具有不同索引的 Pandas 数据帧生成唯一值对? - How efficiently generate unique pairs of values from a Pandas data frame with different indexes? 如何从给定特定元素的数据框中获取特定的列键? - How to get specific column key from data frame given a particular element? 从多个列表中获取唯一元素列表,并将唯一元素列计数为数据框中的列表 - Get the list of unique elements from multiple list and count of unique elements-column as list in data frame 如何在给定的数据帧中划分列“位置”? - How to divide the column 'location' in given data frame? 如何在数据框中创建(x,y)对的列 - How to create a column of (x,y) pairs in a data frame 如何获得数据框中所有唯一的单词? - How to get all the unique words in the data frame? 从列中的唯一值创建一个较小的数据框 - create a smaller data frame from unique value from a column python - 如何使用python中另一个数据框中的列中的重复值为唯一行对数据框进行子集化? - How can I subset a data frame for unique rows using repeating values from a column in another data frame in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM