0

I need to replace null values in string type columns to be 0. Data looks like this:

df.groupBy('content').count().show() 
+---------------+------+
|        content| count|
+---------------+------+
|         videos|   754|
|      food-news| 76151|
|           null|    39|
|             uk| 23879|

I have tried this:

df.na.fill(0).show()

But this piece of code only takes care of int type columns. How can I replace it for string type columns?

Thank you.

2 Answers 2

1

Fill with a string '0' too:

df = df.na.fill(0).na.fill('0')
Sign up to request clarification or add additional context in comments.

3 Comments

After applying your peice of code, I ran df.groupBy('my_column').count().show() I still see null in the list...
@Chique_Code Did you do df = df.na.fill('0')? Simply doing df.na.fill('0') wil not modify df. I've updated my answer to reflect this.
Ah! That did the trick. Sorry about that. I have accepted and upvoted the answer.
0

In below code all int values will be replaced by 0 and string values to ' '(blank).

df = df.na.fill(0).na.fill(' ')

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.