Home > Software design >  trying convert a dataframe column which is all str into float, but getting all NaN in the process (P
trying convert a dataframe column which is all str into float, but getting all NaN in the process (P

Time:12-29

I am fairly new to python I am trying to convert a set of data which is all strings into floats. But when I try I only get nan.

The commands I used are:

print(df['Rendite (%)'].dtypes) ---> Object

df['Rendite (%)'] = float(df['Rendite (%)']) ---> gives me the follwoing error: TypeError: cannot convert the series to <class 'float'>

if I try: df['Rendite (%)'] = pd.to_numeric(df['Rendite (%)'],errors='coerce') print(df['Rendite (%)'].dtypes)

--->float64

But they are all NaN

I put a screenshot of the dataset below:

(It's from morningstar)

Does anybody have an idea on how to fix this?

Thanks

Screenshot of data

CodePudding user response:

You can try this solution:

df['Rendite (%)'] = df['Rendite (%)'].astype('float64')

Think it helps

CodePudding user response:

If you print the df['Rendite (%)'].dtypes is the series of data which must be divided by , character first, and then you can change them to float.

You can use the .split function in the str library or CSV reader to read CSV file and separate each data

  • Related