0

I try tro convert null values into a string variable as x. The reason is that this data frame should be imported to power Bi to make visualisations. We aim to calculate a box plot and my idea is that if there is a x, then these calues will no be included in the calculation. In this way, we can avoid overestimation in the calculation. Is it a good idea to replace null values with x? Or is there a better approach?

data = [["1", "Amit", "DU", "I", "8", "6"],
        ["2", "Mohit", "DU", "I", "4", "2"],
        ["3", "rohith", "BHU", "I", "5", "3"],
        ["4", "sridevi", "LPU", "I", "1", "6"],
        ["1", "sravan", "KLMP", "M", "2", "4"],
        ["5", "gnanesh", "IIT", "M", "null", "8"],
       ["6", "gnadesh", "KLM", "c", "10", "null"]]

columns = ['ID', 'NAME', 'college', 'metric', 'x', 'y']


dataframe = spark.createDataFrame(data, columns)

Actual output

+---+-------+-------+------+----+-----+
| ID|   NAME|college|metric|  x |  y  |
+---+-------+-------+------+----+----+
|  1|   Amit|     DU|     I|  8 |  6 |
|  2|  Mohit|     DU|     I|  4 |  2 |
|  3| rohith|    BHU|     I|  5 |  3 |
|  4|sridevi|    LPU|     I|  1 |  6 |
|  1| sravan|   KLMP|     M|  2 |  4 |
|  5|gnanesh|    IIT|     M|null|  8 |
|  6|gnadesh|    KLM|     c| 10 |null|
+---+-------+-------+------+----+----+

Desired output

+---+-------+-------+------+----+-----+
| ID|   NAME|college|metric|  x |  y  |
+---+-------+-------+------+----+----+
|  1|   Amit|     DU|     I|  8 |  6 |
|  2|  Mohit|     DU|     I|  4 |  2 |
|  3| rohith|    BHU|     I|  5 |  3 |
|  4|sridevi|    LPU|     I|  1 |  6 |
|  1| sravan|   KLMP|     M|  2 |  4 |
|  5|gnanesh|    IIT|     M|  x |  8 |
|  6|gnadesh|    KLM|     c| 10 |  x |
+---+-------+-------+------+----+----+

I tried this code, but i does not work with string, but only with numbers


data = data.fillna({'y':'x'})

1
  • check if the null is written as a string? if yes, then use when().otherwise() Commented Dec 5, 2022 at 9:23

1 Answer 1

1

You can use the following:

data = data.fillna('x')

Please ensure that both your columns x and y are of Stringtype():

data = data.withColumns('x',col(('x').cast('string'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.