-1

so this is the line that is getting the error :

footballers['Value (M)'] = footballers['Value (M)'].astype(float)

where the footballers[] is defined as:

    footballers = df.copy()
footballers['Unit'] = df['Value'].str[-1]
footballers['Value (M)'] = np.where(footballers['Unit'] == '0', 0, 
                                    footballers['Value'].str[1:-1].replace(r'[a-zA-Z]',''))
footballers['Value (M)'] = footballers['Value (M)'].str[1:-1].astype(float)

footballers['Value (M)'] = np.where(footballers['Unit'] == 'M', 
                                    footballers['Value (M)'], 
                                    footballers['Value (M)']/1000)
footballers = footballers.assign(Value=footballers['Value (M)'],
                                 Position=footballers['Preferred Positions'].str.split().str[0])

The error which i am getting is :

ValueError: could not convert string to float:

this is row - enter image description here

Jonardan Cena
  • 373
  • 3
  • 11
  • If your footballers['Unit'] == '0' is false, then you will footballers['Value'].str[1:-1].replace(r'[a-zA-Z]',''). This does not give you a value that can be astype to float. Did you see what's in the dataframe after doing np.where ? – EBDS Sep 29 '21 at 04:51

1 Answers1

0

If your leading and trailing characters footballers['Value'].str[0] and footballers['Value'].str[-1] are not digits, the conversion to float will fail.

Try:

footballers['Value (M)'] = footballers['Value (M)'].str.extractall(r'([\d\.]+)') \
                                                   .astype(float)

If this fails again, try to find bad rows

footballers.loc[pd.to_numeric(footballers['Value (M)'], errors='coerce').isna(),
                'Value (M)']
Corralien
  • 109,409
  • 8
  • 28
  • 52