List and count unique values (MS Excel, Python, and SQL)

List and count unique values (MS Excel, Python, and SQL)

This is a fairly common requirement that until recently had a convoluted solution in Excel. Given a list of values, how do we get a count of the unique values and how do we create a list of unique values?

MS Excel

Start with a list in B2:B20, with B2 as the header and B3:B20 with data. Then, in Excel 365, we count the unique values with

=COUNTA(UNIQUE(B3:B20))        

Even better, suppose the list in column B is a table. Then, we use the below, which will auto-adjust as we add more rows to the table. [Vegetables is the column header for a list of vegetables.]

=COUNTA(UNIQUE(Table1[Vegetables]))        

And we get the list of unique values with

=UNIQUE(Table1[Vegetables])        

SQL

Suppose we have a table Customers with a column fName. Then, we get the count of unique names and the unique names themselves with

select count(distinct fName) "Count of Distinct Names" from dbo.customers;
SELECT distinct fName "Unique Names" FROM dbo.customers;        

Python (with Pandas)

Given a list of vegetables, we use

import pandas as pd
data={"Vegetables":["Spinach","Spinach","Spinach","Kale","Kale","Kale","Kale",
? ? "Potato","Okra","Okra","Carrot","Celery","Arugula","Arugula","Arugula",
? ? "Arugula","Cauliflower","Cauliflower"]}
df=pd.DataFrame(data)
print(df.Vegetables.nunique())
# We can also work without knowing the name of the column. ?See below
print(df[df.columns[0]].unique())        

My previous article: https://www.dhirubhai.net/pulse/excel-large-number-arithmetic-python-tushar-mehta

Julie Basu, PhD

Silicon Valley Technologist | Entrepreneur | CEO | Board Director | R&D Lead at smartQED and ProSolvr

2 年

Simple and powerful!

要查看或添加评论,请登录

Tushar Mehta的更多文章

  • MS Excel: How to develop a recursive function

    MS Excel: How to develop a recursive function

    Introduction A recursive function returns a value that depends on the value that the function returns for another set…

    2 条评论
  • Phone Number Region Lookup

    Phone Number Region Lookup

    Introduction We call people using phone numbers without giving much thought to what the number represents. Each number…

    1 条评论
  • MS Excel - Graph a Spill Range

    MS Excel - Graph a Spill Range

    Introduction to Dynamic Array Formulas and Spill Ranges Microsoft implemented a major improvement in Excel with the…

  • Excel Large Number Arithmetic with Python

    Excel Large Number Arithmetic with Python

    Introduction When it comes to numbers, MS Excel follows the IEEE 754 standard (Floating-point arithmetic may give…

  • MS Excel: Recursive functions

    MS Excel: Recursive functions

    In MS Excel: Functions are first-class objects, we saw how a custom Excel function, using LAMBDA, can call another…

    3 条评论
  • MS Excel: Functions are first-class objects

    MS Excel: Functions are first-class objects

    What makes a function a first-class object? This is almost certainly an advanced discussion. And, we will start with a…

  • Generate unique random integers

    Generate unique random integers

    Generating unique random integers is an important task in various domains. Examples include data sampling, simulations,…

    2 条评论
  • MS Excel: Goal Seek or Algebra

    MS Excel: Goal Seek or Algebra

    Let's start with a question. I want to pay a business through a payment processor such as PayPal.

    1 条评论
  • Calendar for the New Year

    Calendar for the New Year

    For the New Year..

    1 条评论
  • Validating U.S. Bank Routing Numbers

    Validating U.S. Bank Routing Numbers

    In the U.S.

社区洞察

其他会员也浏览了