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

Viewed 19507

I want to print the variable based on the index number based on the following dataset:

enter image description here

Here I used the following code:

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]  )

I get the following error:

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

I know that the error is because the FlightNo variable contains int value. But I don't know how to solve it. Any idea?

2 Answers

If you only want to print, then do this:

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

There is no need to convert the int to str here. You get an error in your statement because you can't concatenate a string and an integer. Try doing "a" + 1 in Python console and see what error it shows.

convert your int to a string:

str(airline.Flightno[zzz])
Related