TypeError:只能连接str(not“数字.int64)至s

2024-05-16 05:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据基于以下数据集的索引号打印变量:

enter image description here

这里我使用了以下代码:

import pandas as pd

airline = pd.read_csv("AIR-LINE.csv")

pnr = input("Enter the PNR Number ")
index = airline.PNRNum[airline.PNRNum==pnr].index.tolist()
zzz = int(index[0])
print( "The flight number is " + airline.FlightNo[zzz]  )

我得到以下错误:

TypeError: can only concatenate str (not "numpy.int64") to str

我知道错误是因为FlightNo变量包含int值。但我不知道怎么解决。有什么想法吗?在


Tags: csv数据代码importpandasindex错误int
2条回答

将int转换为字符串:

str(airline.Flightno[zzz])

如果只想打印,请执行以下操作:

print("The flight number is ", airline.FlightNo[zzz])

这里不需要将int转换为str。语句中出现错误,因为无法连接字符串和整数。尝试在Python控制台中执行"a" + 1,看看它显示了什么错误。在

相关问题 更多 >