0

I have a python pandas dataframe that should print with the following line:

close.loc[stock, str(j)+'_days_ago'] = float(stocks.iloc[[-1 - j]]['Close'].to_string(header=None, index=None))

yet I get the following error:

ValueError: could not convert string to float: '1,971.25'

Any ideas how to fix this error?

1
  • 1
    It can't convert the value to a string. You can try to remove the comma's, then converting to float will be possible. To do this with your series: df['column_name'].str.replace(',', '') Commented Nov 15, 2020 at 14:26

1 Answer 1

1

You have to remove the , form the string (provided your decimal separator is the ..

I.e. use

float('1,971.25'.replace(',', ''))

In your code it should be

close.loc[stock, str(j)+'_days_ago'] = float(stocks.iloc[[-1 - j]]['Close'].to_string(header=None, index=None).replace(',', ''))
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.