Skip to content
Advertisement

How to obtain row percentages of crosstab from a spark dataframe using python?

I used python code:

JavaScript

to create a crosstab from a spark dataframe as follows:

enter image description here

However, I cannot find a code to obtain the row percentages. For example, age 18 row percentages should be 5/12 = 41.7% for ‘no’ and 7/12 = 58.3% for ‘yes’. The sum of 2 percentages is 100%.

May someone advise me in this case? Many thanks in advance.

Advertisement

Answer

Simply add 2 columns using using withColumn and your formula to calculate the percentages:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement