0

It is required to have a method that firstly checks the content of each cell of a dataframe and if the cell contains only spaces and nothing else then fills it with np.nan. To do so, I have written the following method:

def white_space_replacer(df):
    for i in list(df):
        if not is_numeric_dtype(df[i]) and df[i].any().isspace():
            df[i] = df[i].fillna(np.nan)

But it doesn't change anything.

What should be changed in the method to work properly?

Bsh
  • 345
  • 1
  • 9
  • post data not links. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – sammywemmy Feb 23 '20 at 02:33

1 Answers1

1

You can use the below code to fill in the empty spaces with NaN directly while reading a .csv file using pandas.

data = pd.read_csv('test.csv', skipinitialspace=True)
double-beep
  • 5,031
  • 17
  • 33
  • 41
Jai
  • 819
  • 7
  • 17