Home > OS >  Convert row list of strings to float in pandas
Convert row list of strings to float in pandas

Time:06-22

I have a data frame where each line is a list like in the following image.

The dataframe

I am trying to convert each list for each row in float to make some calculations and graphs, but I am getting the

ValueError: could not convert string to float: 'Location'

when I am using

df_float = map(float, df)
df_float = list(df_float)
df_float

This is what .info() returns:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 50 entries, 0 to 49
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype 
---  ------    --------------  ----- 
 0   Location  50 non-null     object
 1   DOY       50 non-null     object
 2   SOB       50 non-null     object
dtypes: object(3)
memory usage: 1.3  KB

CodePudding user response:

try something like

df['Location'] = df['Location'].astype(float) #(or int)

before the code that maps, because as a string it's not castable I guess.

Have not run it, just offering a potential help.

CodePudding user response:

try this:

df.Location = df.Location.apply(lambda x: [float(i) for i in x]) #or int

this will convert every string in the Location's lists to float

  • Related