[英]How to get the unique pairs from the given data frame column with file handling?
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如果我使用手动数据,那么数据有什么问题,为什么不在数据框中工作
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.