- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我做到了
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 ']]
我需要这些是 float 类型。这不可能是重复的,因为 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.]]
这些值不正确,需要调整。
最佳答案
你可以这样做:
print(df.replace('[\$,]', '', regex=True).astype(float))
然后你就会得到想要的。
更新:
做:
print(df.replace('[^0-9.]', '', regex=True).astype(float))
然后:
print(df)
根据需要。
关于python - 字符串到浮点类型的 Ndarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53329551/
在这个例子中,我有一个一维 ndarray 列表,长度为 9,列表有 9 个元素,每个元素有 shape=(2048,) ,所以完全9 * (2048,) , 我得到这些 ndarray来自 mxne
Python/NumPy 中的三种“全部”方法有什么区别?性能差异的原因是什么? ndarray.all() 是否总是三者中最快的? 这是我运行的计时测试: In [59]: a = np.full(
我似乎在 this 中遇到了 XY 问题关于如何判断数组是否共享相同内存的问题。我检查的方式是错误的,我不知道为什么。 举几个例子 test = np.ones((3,3)) test2 = np.a
我在使用 mxnet 进行基本 IO 方面遇到问题。我正在尝试使用 mxnet.io.NDArrayIter 读取内存数据集以在 mxnet 中进行训练。我有下面的代码(为了简洁而精简),它预处理代码
首先,这不是作业问题;而是作业问题。它是与我的工作相关的实际问题的抽象。我真的很感谢所有的意见! 我需要运行类似于下面的计算,按顺序运行数万次,它的计算时间显着影响我的模拟的总持续时间: 在这个抽象中
这个问题在这里已经有了答案: Index multidimensional array with index array (1 个回答) 关闭 2 年前。 我想根据一些坐标从 src ndarray
因此 numpy ndarray 非常方便,因为您只需为任何一维函数 f 和任何 ndarray A 输入 f(A) 即可,它将按元素应用 f。有人告诉我,这也是将函数应用于 ndarray 并避免
我有一个 ndarray 字符串。我想将它转换回 ndarray。我尝试了 newval = np.fromstring(val, dtype=float) 。但它给出了ValueError:字符串大
我正在 python/numpy 中做一些机器学习工作,其中我想用一维 ndarray 索引一个二维 ndarray,这样我就可以得到一个带有索引值的一维数组。 我让它与一些丑陋的代码一起工作,我想知
我想根据某些维度的索引位置数组提取 numpy ndarray 的一部分。让我用一个例子来说明这一点 示例数据 dummy = np.random.rand(5,2,100) X = np.array
这个问题在这里已经有了答案: Find the row indexes of several values in a numpy array (8 个答案) 关闭 2 年前。 我有 a = np.a
我想提取 numpy.ndarray 的第一个轴成为numpy.ndarray的列表. 例如,arr_A包含形状为 (3, 100, 200) 的 numpy ndarray,它将转换为形状为 (10
我一直在尝试用 ndarrays 的 numpy ndarray 转换数组数组。 这是我的数据类型: dt = 'i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,i8,f8,i
我目前正在研究一种可以拆分 numpy.ndarray 的方法进入给定数量的子阵列,只要该数量小于窗口移动的轴。 示例: 给定一个形状为 (15, 40, 3) 的 numpy.ndarray 我想分
我用 arcpy 模块创建了一个 NumPy 结构化数组(称为 arr): arr = arcpy.da.FeatureClassToNumPyArray('MPtest','SHAPE@XYZ',e
是否可以将一个 numpy 数组的特定行的引用存储在另一个 numpy 数组中? 我有一个二维节点数组,例如 nodes = np.array([[1, 2], [2, 3], [3, 4], [4,
我有两个长度相同的 pandas 系列,如下所示: S1 = 0 -0.483415 1 -0.514082 2 -0.515724 3 -0.519375 4
当使用 numpy 时,假设我有一个任意的、以前创建的名为 my_ndarray 的 ndarray。如果可能的话,我希望能够执行以下操作...... my_bytes = my_ndarray.to
我在尝试使用 numpy 打乱多维数组时遇到问题。可以使用以下代码重现该问题: import numpy as np s=(300000, 3000) n=s[0] print ("Allocate"
当您调用 DataFrame.to_numpy() 时,pandas 将找到可以容纳 DataFrame 中所有数据类型的 NumPy 数据类型。但是如何进行反向操作呢? 我有一个“numpy.nda
我是一名优秀的程序员,十分优秀!