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)]
Try Groupby.groups:
list(people.groupby(['PostalCode', 'Age']).groups)
[out]
[('ab 11', 10), ('ab 22', 14)]
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')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With