Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
hannahcochran
Contributor

Counting unique values in a column in pandas dataframe like in Qlik?

If I have a table like this:

df = pd.DataFrame({
'hID': [101, 102, 103, 101, 102, 104, 105, 101],
'dID': [10, 11, 12, 10, 11, 10, 12, 10],
'uID': ['James', 'Henry', 'Abe', 'James', 'Henry', 'Brian', 'Claude', 'James'],
'mID': ['A', 'B', 'A', 'B', 'A', 'A', 'A', 'C']
})

I can do count(distinct hID) in Qlik to come up with count of 5 for unique hID. How do I do that in python using a pandas dataframe? Or maybe a numpy array? Similarly, if were to do count(hID) I will get 8 in Qlik. What is the equivalent way to do it in pandas?

Labels (1)
2 Replies
edwin
Master II

i think you posted in the wrong forum.  you are not asking for a solution in QLik but in a different tool and hoping someone from the QLik community also uses that tool which may be a small probability but good luck to that.

one thing i would comment on when you say count(hid) will yeild 8 - i would say it depends on you data model.  

MarcoWedel

I agree that this is the wrong place to ask python related questions.
(but you could use df['hID'].nunique() in this case 😉