Nicolas Gervais
Nicolas Gervais

Reputation: 36664

Pandas: "ValueError: could not convert string to float: " (empty string)

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.

Upvotes: 3

Views: 6132

Answers (1)

Asnim P Ansari
Asnim P Ansari

Reputation: 2487

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

df.replace(r'^\s*$', np.nan, regex=True)

Upvotes: 3

Related Questions