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)