Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert pandas dataframe into a list of unique tuple

What is the most efficient way to convert a pandas dataframe into a list of unique tuple? In the code below I am trying to extract a list of tuples containing all the unique PostalCode and Age.

from typing import NamedTuple, Sequence, Tuple

import pandas as pd

data = [["tom", 10, "ab 11"], ["nick", 14, "ab 22"], ["juli", 10, "ab 11"]]
people = pd.DataFrame(data, columns=["Name", "Age", "PostalCode"])

subset = pd.unique(people[["PostalCode", "Age"]].values.ravel())

# This produce an error
output = list(subset.itertuples(index=False))

# Expected output
# [('ab 11', 10), ('ab 22', 14)]
like image 810
Michael Avatar asked Oct 21 '25 04:10

Michael


2 Answers

Try Groupby.groups:

list(people.groupby(['PostalCode', 'Age']).groups)

[out]

[('ab 11', 10), ('ab 22', 14)]
like image 114
Chris Adams Avatar answered Oct 22 '25 19:10

Chris Adams


I think you need:

xyz = list(set((i,j) for i,j in zip(people["Age"], people["PostalCode"])))

print(xyz)

Output:

[(10, 'ab 11'), (14, 'ab 22')]
like image 41
Sociopath Avatar answered Oct 22 '25 17:10

Sociopath