问题
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