我可以通过以下方式重现您的错误消息:
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'
在 Python(不是 numpy 或 pandas)中,列表或其他序列乘以整数会复制该序列:
In [268]: [1,2,3]*3
Out[268]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
如果
df['first_column'] * df['second column']
正在产生错误,那么一个术语是一个序列(例如列表),另一个是浮点数。另一种可能性是一个对象 dtype 数组,并且包含一个或多个列表。
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'
它甚至可以是浮点数和列表的混合体,对数字执行 number * 并在列表上进行复制。
In [283]: np.array([(2,3),(3,),12])*np.array([[3],[2]])
Out[283]:
array([[(2, 3, 2, 3, 2, 3), (3, 3, 3), 36],
[(2, 3, 2, 3), (3, 3), 24]], dtype=object)
更有可能是一个包含数字和字符串的对象数组(或数据系列):
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'