Home > Mobile >  replace empty values with a string pandas
replace empty values with a string pandas

Time:11-26

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

CodePudding user response:

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
  • Related