-1

I want to convert the columns of this list to numeric using the pandas.to_numeric function, catching the errors and force the conversion by supplying errors='coerce' as an argument for pandas.to_numeric :

wrong_type_columns = ['DewPointHighF', 'DewPointAvgF', 'DewPointLowF', 'HumidityHighPercent', 
                      'HumidityAvgPercent', 'HumidityLowPercent', 'SeaLevelPressureHighInches', 
                      'SeaLevelPressureAvgInches' ,'SeaLevelPressureLowInches', VisibilityHighMiles',
                      'VisibilityAvgMiles', 'VisibilityLowMiles', 'WindHighMPH', 'WindAvgMPH', 
                      'WindGustMPH', 'PrecipitationSumInches']

I'll try this code:

wrong_type_columns = wrong_type_columns.apply(pd.to_numeric, errors='coerce', axis=1).astype(int)

but i get this error:

AttributeError: 'list' object has no attribute 'apply'
wjandrea
  • 28,235
  • 9
  • 60
  • 81

1 Answers1

1

Try this

cols = ['DewPointHighF', 'DewPointAvgF', 'DewPointLowF', 'HumidityHighPercent', 
        'HumidityAvgPercent', 'HumidityLowPercent', 'SeaLevelPressureHighInches', 
        'SeaLevelPressureAvgInches' ,'SeaLevelPressureLowInches', 'VisibilityHighMiles',
        'VisibilityAvgMiles', 'VisibilityLowMiles', 'WindHighMPH', 'WindAvgMPH', 
        'WindGustMPH', 'PrecipitationSumInches']
df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

to see if this gives you what you want.

Paul Brennan
  • 2,638
  • 4
  • 19
  • 26