Character string not recognized in if statement

I cannot get my coding to recognize a character string in the execution of an if statement. I have tried my best to figure this out with no luck. Please help a newbie.

import numpy as np
import pandas as pd
x20 = pd.read_csv('C:/Ron Play.csv',
                delimiter=',',
                header=0,
                usecols=[0]
                )
print(x20)
x2 = np.array(n*[[1]])
for i in range(n):
    if x20[i, 0] == 'Passenger': x2[i, 0] = 0
    continue
print(x2)

Traceback (most recent call last):

File “C:\Users\Ronald.Fleming\AppData\Local\Programs\Spyder\pkgs\pandas\core\indexes\base.py”, line 3080, in get_loc
return self._engine.get_loc(casted_key)

File “pandas_libs\index.pyx”, line 70, in pandas._libs.index.IndexEngine.get_loc

File “pandas_libs\index.pyx”, line 101, in pandas._libs.index.IndexEngine.get_loc

File “pandas_libs\hashtable_class_helper.pxi”, line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item

File “pandas_libs\hashtable_class_helper.pxi”, line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: (0, 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

File “”, line 2, in
if x20[i, 0] == ‘Passenger’: x2[i, 0] = 0

File “C:\Users\Ronald.Fleming\AppData\Local\Programs\Spyder\pkgs\pandas\core\frame.py”, line 3024, in getitem
indexer = self.columns.get_loc(key)

File “C:\Users\Ronald.Fleming\AppData\Local\Programs\Spyder\pkgs\pandas\core\indexes\base.py”, line 3082, in get_loc
raise KeyError(key) from err

KeyError: (0, 0)

The error has nothing to do with the string.

Unfortunately it is sometimes hard to read pandas tracebacks, but the failure here is from trying to look up x20[i, 0] when i = 0 (the very first iteration of the loop).

I don’t know what keys are available in x20. You print it, so perhaps you can look at what is printed.

A total guess: maybe you need to start at 1 not 0? for i in range(1, n):.

Another total guess: maybe the keys need to be strings?

for i in range(n):
    if x20[str(i), '0'] == 'Passenger': x2[i, 0] = 0

But I’m literally guessing.

Thank you for helping me out.

if x20[str(i), '0'] == 'Passenger': x2[i, 0] = 0

did not work, but it gave me an idea.

if x20[i,] == 'Aircraft': x2[i,] = 0

does work. Again, thank you for taking the time to respond to my question. I am including a working example if you are interested.

import numpy as np
import pandas as pd

x20 = np.matrix('"Passenger" ; "Passenger" ; "Aircraft" ; "Aircraft" ; "Passenger" ; "Passenger" ; "Passenger" ; "Aircraft"')
print(x20)
nn = len(x20)

x2 = np.array(nn*[[1]])
for i in range(nn):
    if x20[i,] == 'Aircraft': x2[i,] = 0
    continue
#print(x2)