Is there an easy way to pull out the distinct combinations of values in a dataframe? I've used pd.Series.unique() for single columns, but what about multiple columns?
Example data:
df = pd.DataFrame(data=[[1, 'a'], [2, 'a'], [3, 'b'], [3, 'b'], [1, 'b'], [1, 'b']],
columns=['number', 'letter'])
Expected output:
(1, a)
(2, a)
(3, b)
(1, b)
Ideally, I'd like a separate Series object of tuples with the distinct values.
You can zip the columns and create a set:
>>> set(zip(df.number, df.letter))
{(1, 'a'), (1, 'b'), (2, 'a'), (3, 'b')}
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