提问人:Peddi Akhilesh 提问时间:10/31/2023 最后编辑:Andrej KeselyPeddi Akhilesh 更新时间:10/31/2023 访问量:50
Python 类型错误中的列表推导式任何人都请回答
List Comprehension in Python Type Error Cany anyone answer please
问:
# given code
# Import the data file and have a look!
import pandas as pd
s = pd.read_csv("celsius.csv")
s.head()
def converter(x):
return 1.8*x + 32
f = [converter(p) for p in s]
TypeError Traceback (most recent call last)
<ipython-input-16-e542ae5b13a4> in <cell line: 11>()
9 return 1.8*x + 32
10
---> 11 f = [converter(p) for p in s]
1 frames
<ipython-input-16-e542ae5b13a4> in converter(x)
7
8 def converter(x):
----> 9 return 1.8*x + 32
10
11 f = [converter(p) for p in s]
TypeError: can't multiply sequence by non-int of type 'float'
谁能告诉我为什么它是错误的?
答: 暂无答案
评论
p
可能不是你所期望的数字。是字符串吗?如果是这样 - 将其转换为数字类型,具体取决于您拥有的数据。int(p)
float(p)
s
columnname