Ndarray строк в тип с плавающей точкой - PullRequest
0 голосов
/ 16 ноября 2018

Я сделал это

cf = df.iloc[:,1:12]
cf = cf.values
print(cf)

, что дает мне

[['$0.00 ' '$771.98 ' '$0.00 ' ..., '$771.98 ' '$0.00 ' '$1,543.96 ']
 ['$1,320.83 ' '$4,782.33 ' '$1,320.83 ' ..., '$1,954.45 ' '$0.00 '
  '$1,954.45 ']
 ['$2,043.61 ' '$0.00 ' '$4,087.22 ' ..., '$4,662.30 ' '$2,907.82 '
  '$1,549.53 ']
 ..., 
 ['$427.60 ' '$0.00 ' '$427.60 ' ..., '$427.60 ' '$0.00 ' '$427.60 ']
 ['$868.58 ' '$1,737.16 ' '$0.00 ' ..., '$868.58 ' '$868.58 ' '$868.58 ']
 ['$0.00 ' '$1,590.07 ' '$0.00 ' ..., '$787.75 ' '$0.00 ' '$0.00 ']]

Мне нужно, чтобы они были плавающих типов. Это не является возможным дубликатом, поскольку переменная cf является NDarray, а не фреймом данных.

Я пытался сделать это:

cf = df.iloc[:,1:12].replace('[\$,]', '', regex=True).astype(float)
cf = cf.values
print(cf)

Но я получаю эти ошибки:

ValueError                                Traceback (most recent call last)
<ipython-input-152-f5009cb31652> in <module>()
      1 # Place as_of_date and cash flows into an unordered_map or dictionary
----> 2 cf = df.iloc[:,1:12].replace('[\$,]', '', regex=True).astype(float)
      3 cf = cf.values
      4 print(cf)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
     89                 else:
     90                     kwargs[new_arg_name] = new_arg_value
---> 91             return func(*args, **kwargs)
     92         return wrapper
     93     return _deprecate_kwarg

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\generic.py in astype(self, dtype, copy, errors, **kwargs)
   3408         # else, only a single dtype is given
   3409         new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 3410                                      **kwargs)
   3411         return self._constructor(new_data).__finalize__(self)
   3412 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in astype(self, dtype, **kwargs)
   3222 
   3223     def astype(self, dtype, **kwargs):
-> 3224         return self.apply('astype', dtype=dtype, **kwargs)
   3225 
   3226     def convert(self, **kwargs):

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3089 
   3090             kwargs['mgr'] = self
-> 3091             applied = getattr(b, f)(**kwargs)
   3092             result_blocks = _extend_blocks(applied, result_blocks)
   3093 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    469     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    470         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 471                             **kwargs)
    472 
    473     def _astype(self, dtype, copy=False, errors='raise', values=None,

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, raise_on_error, **kwargs)
    519 
    520                 # _astype_nansafe works fine with 1-d only
--> 521                 values = astype_nansafe(values.ravel(), dtype, copy=True)
    522                 values = values.reshape(self.shape)
    523 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy)
    634 
    635     if copy:
--> 636         return arr.astype(dtype)
    637     return arr.view(dtype)
    638 

ValueError: could not convert string to float: '(641.99)'

Я не уверен, как это исправить, пожалуйста, пересмотрите ответы, чтобы я мог положить это в порядок и перейти к чему-то еще.

Из предложенного ответа я сделал это

cf = df.iloc[:,1:12].replace('[^0-9]', '', regex=True).astype(float)
cf = cf.values
print(cf)

, который дает мне это

[[      0.   77198.       0. ...,   77198.       0.  154396.]
 [ 132083.  478233.  132083. ...,  195445.       0.  195445.]
 [ 204361.       0.  408722. ...,  466230.  290782.  154953.]
 ..., 
 [  42760.       0.   42760. ...,   42760.       0.   42760.]
 [  86858.  173716.       0. ...,   86858.   86858.   86858.]
 [      0.  159007.       0. ...,   78775.       0.       0.]]

Значения неверны и требуют корректировки.

1 Ответ

0 голосов
/ 16 ноября 2018

Вы можете сделать:

print(df.replace('[\$,]', '', regex=True).astype(float))

Тогда вы получите нужный.

Обновление:

DO:

print(df.replace('[^0-9.]', '', regex=True).astype(float))

Тогда:

print(df)

IS по желанию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...