Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 840
Nicolas Gervais Avatar asked Jun 21 '26 17:06

Nicolas Gervais


1 Answers

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

df.replace(r'^\s*$', np.nan, regex=True)
like image 69
Asnim P Ansari Avatar answered Jun 24 '26 06:06

Asnim P Ansari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!