Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两列中的值相乘:TypeError:can';t将序列乘以类型为'的非整数;浮动';_Python_Pandas_Numpy - Fatal编程技术网

Python 将两列中的值相乘:TypeError:can';t将序列乘以类型为'的非整数;浮动';

Python 将两列中的值相乘:TypeError:can';t将序列乘以类型为'的非整数;浮动';,python,pandas,numpy,Python,Pandas,Numpy,我试图将两列中的值相乘 第一列中的元素类型是'numpy.float64' 第二列的是'float' 当我这样做的时候 new_column = df['first_column'] * df['second column'] 我明白了 'TypeError:无法将序列与'float'类型的非整数相乘' 我真的不明白为什么不能将numpy.float64和float的值相乘。它们不是彼此相似且可重复使用吗?您可以尝试这样的不安全铸造: numpy.multiply(A, B, out=A, c

我试图将两列中的值相乘

第一列中的元素类型是
'numpy.float64'

第二列的是
'float'

当我这样做的时候

new_column = df['first_column'] * df['second column']
我明白了

'TypeError:无法将序列与'float'类型的非整数相乘'


我真的不明白为什么不能将
numpy.float64
float
的值相乘。它们不是彼此相似且可重复使用吗?

您可以尝试这样的不安全铸造:

numpy.multiply(A, B, out=A, casting='unsafe')

您可以尝试这样的不安全铸造:

numpy.multiply(A, B, out=A, casting='unsafe')

我可以通过以下方式复制您的错误消息:

In [267]: [1,2,3]*3.43
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-267-fc9c3bc4b243> in <module>()
----> 1 [1,2,3]*3.43

TypeError: can't multiply sequence by non-int of type 'float'
如果

产生错误时,一个术语是序列(例如列表),另一个术语是浮点。另一种可能是,其中一个是对象数据类型数组,包含一个或多个列表

In [271]: np.array([(2,3),(3,)])*3
Out[271]: array([(2, 3, 2, 3, 2, 3), (3, 3, 3)], dtype=object)
In [272]: np.array([(2,3),(3,)])*3.34
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-272-c3152ad55f88> in <module>()
----> 1 np.array([(2,3),(3,)])*3.34

TypeError: can't multiply sequence by non-int of type 'float'
更有可能是一个混合了数字和字符串的对象数组(或数据系列):

In [287]: np.array(['astring',12],dtype=object)*np.array([[3]])
Out[287]: array([['astringastringastring', 36]], dtype=object)
In [288]: np.array(['astring',12],dtype=object)*np.array([[3.23]])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-288-5a02408d1a73> in <module>()
----> 1 np.array(['astring',12],dtype=object)*np.array([[3.23]])

TypeError: can't multiply sequence by non-int of type 'float'
[287]中的
:np.array(['astring',12],dtype=object)*np.array([[3]]
Out[287]:数组([[AstringStringAstring',36]],dtype=object)
在[288]中:np.array(['astring',12],dtype=object)*np.array([[3.23]]
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 np.数组(['astring',12],dtype=object)*np.数组([[3.23]])
TypeError:无法将序列与“float”类型的非int相乘

我可以通过以下方式重现您的错误消息:

In [267]: [1,2,3]*3.43
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-267-fc9c3bc4b243> in <module>()
----> 1 [1,2,3]*3.43

TypeError: can't multiply sequence by non-int of type 'float'
如果

产生错误时,一个术语是序列(例如列表),另一个术语是浮点。另一种可能是,其中一个是对象数据类型数组,包含一个或多个列表

In [271]: np.array([(2,3),(3,)])*3
Out[271]: array([(2, 3, 2, 3, 2, 3), (3, 3, 3)], dtype=object)
In [272]: np.array([(2,3),(3,)])*3.34
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-272-c3152ad55f88> in <module>()
----> 1 np.array([(2,3),(3,)])*3.34

TypeError: can't multiply sequence by non-int of type 'float'
更有可能是一个混合了数字和字符串的对象数组(或数据系列):

In [287]: np.array(['astring',12],dtype=object)*np.array([[3]])
Out[287]: array([['astringastringastring', 36]], dtype=object)
In [288]: np.array(['astring',12],dtype=object)*np.array([[3.23]])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-288-5a02408d1a73> in <module>()
----> 1 np.array(['astring',12],dtype=object)*np.array([[3.23]])

TypeError: can't multiply sequence by non-int of type 'float'
[287]中的
:np.array(['astring',12],dtype=object)*np.array([[3]]
Out[287]:数组([[AstringStringAstring',36]],dtype=object)
在[288]中:np.array(['astring',12],dtype=object)*np.array([[3.23]]
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 np.数组(['astring',12],dtype=object)*np.数组([[3.23]])
TypeError:无法将序列与“float”类型的非int相乘

这个问题解释了如何将numpy类型转换为本地python类型:您没有试图将
numpy.float64
float
的值相乘,这就是问题所在。我猜第一组值是一个
列表
,而不是一个numpy数组标记。第一组和第二组值的类型都是“pandas.core.series.series”。您当然可以使用
zip
以列表理解的方式浏览这两个系列,并分别乘以每个元素。我不熟悉pandas,因此不知道是否还有其他选项。能否提供一个复制错误的示例数据帧。此问题说明如何将numpy类型转换为本机python类型:您没有尝试将
numpy.float64
float
的值相乘,这就是问题所在。我猜第一组值是一个
列表
,而不是一个numpy数组标记。第一组和第二组值的类型都是“pandas.core.series.series”。您当然可以使用
zip
以列表理解的方式浏览这两个系列,并分别乘以每个元素。我不熟悉pandas,所以我不知道是否还有其他选择。你能提供一个复制错误的示例数据帧吗。我尝试过,但我得到了相同的错误:TypeError:can not multiply sequence by non int of type'float'
casting
无法绕过我识别的Python序列错误。我尝试过,但是我得到了同样的错误:TypeError:cannotmultiplesequence乘以类型为'float'的非int
casting
无法绕过我识别的Python序列错误。