0

How to count a number of unique values in multiple columns in Python, pandas etc. I can do for one column using "nunique" function. I need something like:

print("Number of unique values in Var1", DF.var1.nunique(),sep="= ").

For all the variables in the dataset. Something like a loop or apply function maybe. I tried a lot of things failed to get what I desired.

Thanks for the help!

gil.fernandes
  • 12,978
  • 5
  • 63
  • 76
pras
  • 49
  • 2
  • 10

1 Answers1

4

You want to print number of unique values per column, so use:

for k, v in df.nunique().to_dict().items():
    print('{}={}'.format(k,v))
halfer
  • 19,824
  • 17
  • 99
  • 186
zipa
  • 27,316
  • 6
  • 40
  • 58