cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Show all distinct values per column in dataframe Problem Statement:I want to see all the distinct values per column for my entire table, but a SQL que...

Artem_Yevtushen
New Contributor III

Show all distinct values per column in dataframe

Problem Statement:

I want to see all the distinct values per column for my entire table, but a SQL query with a collect_set() on every column is not dynamic and too long to write.

Use this code to show the output below:

%python
 
from pyspark.sql.functions import col, collect_set
 
distincts = df.agg(*(collect_set(col(c)).alias(c) for c in df.columns))
distincts.display()

collect set table

1 REPLY 1

Anonymous
Not applicable

@Artem Yevtushenko​ - This is great! Thank you for sharing!

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!