7

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?

Jarvis
  • 8,494
  • 3
  • 27
  • 58
Prakash Natarajan
  • 138
  • 1
  • 1
  • 11

2 Answers2

6

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.

Jarvis
  • 8,494
  • 3
  • 27
  • 58
  • 1
    No it throws the same error. Should use `str(airline.FlightNo[zzz]`. – Prakash Natarajan Dec 19 '18 at 07:26
  • Did you replace `+` by `,`? @PrakashNatarajan – Jarvis Dec 19 '18 at 07:27
  • You can't concatenate different data-type objects. Both objects need to be of the type `str`. But when you print using a `,`, it automatically formats the integer. Please mark the answer as accepted if it solved your issue. @PrakashNatarajan – Jarvis Dec 19 '18 at 07:30
  • Also i want to seperate the wrongly entered `PNRNum` during the input of the PNR Number. I used `if airline.PNRNum!=pnr: print("Enter correct PNR Number.")` But it throws error. How could I check if the entered value is not present in the specific column? – Prakash Natarajan Dec 19 '18 at 07:48
  • 1
    @PrakashNatarajan See [this](https://stackoverflow.com/questions/21319929/how-to-determine-whether-a-pandas-column-contains-a-particular-value). – Jarvis Dec 19 '18 at 08:19
  • 1
    Thanks mate!! That's exactly what i'm looking for. – Prakash Natarajan Dec 21 '18 at 05:46
1

convert your int to a string:

str(airline.Flightno[zzz])
QED
  • 9,803
  • 7
  • 50
  • 87