3

I always convert the columns into floats, by

prices['Open'] = prices['Open'].apply(lambda x: float(x))
prices['Close'] = prices['Close'].apply(lambda x: float(x))
prices['High'] = prices['High'].apply(lambda x: float(x))
prices['Low'] = prices['Low'].apply(lambda x: float(x))
prices['Volume'] = prices['Volume'].apply(lambda x: float(x))
prices['Market cap'] = prices['Market cap'].apply(lambda x: float(x))

I want to be able to do this in one line, i tried using

prices[['Open', 'Close', 'High', 'Low', 'Volume', 'Market cap']] = prices[['Open', 'Close', 'High', 'Low', 'Volume', 'Market cap']].apply(lambda x: float(x))

but it gives me an error msg:

TypeError: cannot convert the series to <class 'float'>

any help will be appreciated!

1
  • 1
    prices[column_list] = prices[column_list].astype(float) Commented Mar 5, 2022 at 12:13

2 Answers 2

6

You're looking for .applymap:

prices[list_of_columns].applymap(lambda x: float(x))

Also, if you're really trying to just convert the values into floats, just use .astype:

prices[list_of_colums] = prices[list_of_columns].astype(float)
Sign up to request clarification or add additional context in comments.

Comments

0

You can use astype() to convert the data type to float in a single line:

prices = prices.astype({"Open":'float', "Close":'float', "Low":'float', "Volume":'float', "Market cap":'float'})  

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.