How to calculate the counts of each distinct value in column for all the columns in a pyspark dataframe? This is my input dataframe:
spark.table("table1").show()
+-------+---------+------- +--------+
|col1 | col2 | col3 | col4 |
+-------+---------+------- +--------+
|aa | ss | sss | jjj |
|bb | 123 | 1203 | uuu |
|null | 123 | null | zzz |
|null | 123 | 1203 | 6543 |
+-------+---------+--------+--------+
I need the final output data frame some thing like this:
+-----------+-------------+-------+-------+------------+
|table_name | Column_name | Value | count | percentage |
+-----------+-------------+-------+-------+------------+
|table1 | col1 | aa | 1 | |
|table1 | col1 | bb | 1 | |
|table1 | col1 | null | 2 | |
|table1 | col2 | ss | 1 | |
|table1 | col2 | 123 | 3 | |
|table1 | col3 | sss | 1 | |
|table1 | col3 | 1203 | 2 | |
|table1 | col3 | null | 1 | |
|table1 | col4 | jjj | 1 | |
|table1 | col4 | uuu | 1 | |
|table1 | col4 | zzz | 1 | |
|table1 | col4 | 6543 | 1 | |
+-----------+-------------+-------+-------+------------+
I have Python logic for calculating the percentage. Same needs to be implemented in Pyspark
percentages.append(excel['enum'][col].value_counts()[value]/excel['enum'][col].shape[0] * 100)