-2

I have this column

In  [] : parsed_data_prodiTI_smt8['IP'].unique()
Out [] : 
array(['3.69', '4', '3.73', '3.24', '3.56', '3.3', '3.31', '3.74', '3.37',
       '3.19', '3.43', '3.5', '3.64', '3.46', '3.79', '3.14', '3.81',
       '3.21', '3.96', '3.7', '3.6', '3.55', '3.25', '3.07', '3.51',
       '3.67', '2.98', '3', '3.18', '3.53', '3.48', '2.93', '2.82',
       '3.39', '3.36', '1.62', '3.12', '3.27', '3.82', '3.89', '3.47',
       '3.68', '3.95', '3.86', '3.54', '3.63', '3.88', '2.8', '3.9',
       '3.4', '', '3.23', '3.78', '3.45', '3.41', '3.85', '3.52', '3.35', #Notice there's '' in this row
       '3.32', '3.66', '3.38', '3.71', '3.75'], dtype=object)

# I don't know what is it, I thought it's null so I checked it out like this

In  [] : parsed_data_prodiTI_smt8['IP'].isnull().sum()
Out [] : 0 

In  [] : parsed_data_prodiTI_smt8[parsed_data_prodiTI_smt8['IP']=='']['IP'].isnull()
Out [] : False

# Resulting if I do this, it returns error
In  [] : parsed_data_prodiTI_smt8['IP'].astype('float')
Out [] : ValueError: could not convert string to float:

I don't know what it is and apparently it's not null and it's not whitespace too because when I look for it like ==' ' it returns no row. Since I have no idea what that is I don't know how to treat this error. Could someone help? I need to change that column into float and that unidentified '' is blocking my way

3
  • There's an empty string '' (which is different from the single-space string ' ' that you are checking for) in the column, which can't be converted to a float. You'll get the same error with float(''). Commented Jun 1, 2021 at 12:45
  • Okay, after looking for other cases they apparently use .str.extract('(\d)').astype('float') but it cuts the double digits behind the . which I need to capture as it is, ex. 3.41. Is there any way to do this? Commented Jun 1, 2021 at 12:48
  • Does this answer your question? Converting empty strings to 0 using Numpy Commented Jun 1, 2021 at 12:54

1 Answer 1

0

You have to check for omit the ''. Try this

condition = parsed_data_prodiTI_smt8['IP']== ''
array = parsed_data_prodiTI_smt8['IP']
np.where(condition,array,array.astyp('float')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.