1

I have a dataframe which contains empty fields. I want to replace all empty fields with the word 'unknown'. Below is what I tried:

import pandas as pd
import numpy as np

df = pd.DataFrame([
    [-0.532681, 'foo', 0],
    [1.490752, 'bar', 1],
    [-1.387326, 'foo', 2],
    [0.814772, 'baz', ' '],     
    [-0.222552, '   ', 4],
    [-1.176781,  'qux', '  '],         
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))

# replace field that's entirely space (or empty) with unkonwn
value = 'unkown'
df = df.apply(lambda x: np.value if isinstance(x, str) and x.isspace() else x)

print(df)

This is what I got:

    A    B   C
2000-01-01 -0.532681  foo   0
2000-01-02  1.490752  bar   1
2000-01-03 -1.387326  foo   2
2000-01-04  0.814772  baz
2000-01-05 -0.222552        4
2000-01-06 -1.176781  qux

This is what I want

                   A    B   C
2000-01-01 -0.532681  foo   0
2000-01-02  1.490752  bar   1
2000-01-03 -1.387326  foo   2
2000-01-04  0.814772  baz   unkown
2000-01-05 -0.222552  uknown     4
2000-01-06 -1.176781  qux   unkown

1 Answer 1

4

Use ^\s*$ for replace only zero, one or multiple empty strings:

df = df.replace('^\s*$', value, regex=True)
print (df)
                   A       B       C
2000-01-01 -0.532681     foo       0
2000-01-02  1.490752     bar       1
2000-01-03 -1.387326     foo       2
2000-01-04  0.814772     baz  unkown
2000-01-05 -0.222552  unkown       4
2000-01-06 -1.176781     qux  unkown
Sign up to request clarification or add additional context in comments.

4 Comments

What if my dataframe contains NaN's and you want to replace the NaNs with 'unkown'
@Al-Andalus - Then use df = df.fillna(value)
And how about None?
@Al-Andalus - If None is Nonetype working df = df.fillna(value), if None is string need df.replace('^None$', value, regex=True)

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.