3

When trying to convert a Pandas Series to float, I get this error message. It refers to an empty string (" "), which is not a np.nan. Just an empty string.

I tried replacing " " by np.nan, but it doesn't do anything. I also tried replacing " " by "" but it also doesn't do anything.

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [4, " ", 6], [7, 8, 9]]), 
                  columns=['a', 'b', 'c'])

df.astype(float)

ValueError: could not convert string to float:

I want to replace these " " by NaN values, in a large dataframe.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143

1 Answers1

3

You can use df.replace() function to perform this operation

df.replace(r'^\s*$', np.nan, regex=True)
Asnim P Ansari
  • 1,932
  • 1
  • 18
  • 41