I have a very simple question that I have NOT been able to get the answer ‘right’ from many sources (internet and my own NOTES).
I want to count the unique values in each column and the output must be in a DataFrame format:
Example:
Column A: Sibusiso, John, Asande, Thamba, Herry, John, Sibusiso, Themba, Siyabonga, Herry, John, Sibusiso, Asande
Column B: 1, 2, 3, 2, 4, 5, 3, 1000,
Column C: 12, AA, ZZ, 33, 11, 12, AA, BA, 11, ZZ, QA, ED
I want my solution to look like this:
Column A 7
Column B 6
Column C 9
Because there are only 7, 6 and 9 unique values in Column A, Column B, Column C, respectively.
Any solution you might have in mind?
CodePudding user response:
df.apply(lambda x: x.nunique())
You can transform it, if you want the row names to be the column names
CodePudding user response:
just do nunique :
print(df.nunique())