简体   繁体   中英

conversion of nan to numeric

energy[['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']] = energy[['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)

It gives error

Traceback (most recent call last)
pandas/src/inference.pyx in pandas.lib.maybe_convert_numeric (pandas/lib.c:55708)()

ValueError: Unable to parse string "Afghanistan"

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-4-d0b55a0e8afa> in <module>()
      5 energy=energy[['Unnamed: 1','Petajoules','Gigajoules','%']]
      6 energy.columns=['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']
----> 7 energy[['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']]=energy[['Country', 'Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)
      8 energy['Energy Supply']=energy['Energy Supply']*1,000,000
      9 energy

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4150                     if reduce is None:
   4151                         reduce = True
-> 4152                     return self._apply_standard(f, axis, reduce=reduce)
   4153             else:
   4154                 return self._apply_broadcast(f, axis)

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
   4246             try:
   4247                 for i, v in enumerate(series_gen):
-> 4248                     results[i] = func(v)
   4249                     keys.append(v.name)
   4250             except Exception as e:

/opt/conda/lib/python3.6/site-packages/pandas/tools/util.py in to_numeric(arg, errors, downcast)
    193             coerce_numeric = False if errors in ('ignore', 'raise') else True
    194             values = lib.maybe_convert_numeric(values, set(),
--> 195                                                coerce_numeric=coerce_numeric)
    196 
    197     except Exception:

pandas/src/inference.pyx in pandas.lib.maybe_convert_numeric (pandas/lib.c:56097)()

ValueError: ('Unable to parse string "Afghanistan" at position 0', 'occurred at index Country')

Yes, it is definitly an error.

Seriously, considere asking a question, tell what you want to do or what you need to aknowledge to achieve what you're trying.

If the question is " how to make my list energy only numerical", you can try this:

for i in range(len(energy)): 
    if not isinstance(energy[i], (int, float, complex)):
        energy[i] = 0  

You're welcome.

Its because you are trying to convert string to numeric The column 'Country'' obviously might contain strings. Try using without 'Country'

energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']] =  energy[['Energy Supply', 'Energy Supply per Capita', '% Renewable']].replace('...',np.NaN).apply(pd.to_numeric)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM