'Python Pandas - using instead of csv read

I just can't wrap my head around using pandas to recreate the simple script I made using the native python CSV module. The whole point is that with pandas I can specify sheet. I want to work inside a big CSV file, and I can't find this function in CSV module from python.

This is an example of a working script. I search known values to print out values I want about an item.

import csv
with open('myfile.csv', 'rt') as file:
    reader = csv.reader(file)
    for row in reader:
        if Known_Item_Number == row[Column_I_Search_Trough]:
            print('price' row[2])
            print('amount' row[15])
            print('name' row[3])

This way I print only the data I need, and more importantly in order, I need them to be. Trying to do this in pandas proved to be a bit beyond me. I tried to build it from scratch, first at least to find a correct row based on a known item number from a specific column. I tried approach through:

print(df.loc[df['Name_Of_Column_I_Search_Trough'] == 'Known_Item_Number'])

Later I would figure out how to print only data I want in order I want, but it seems I can't even get to do this as I'm getting random errors. Not for every combination of Row/Column - if I try different values for the name of column and known item, sometimes it works, sometimes it does not.

Error below

I guess I may be approaching this all wrong as I am using the perspective of CSV module where everything was nicely punted in a grid with index numbers and easy to search/compare through for values.

Traceback (most recent call last):
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3621, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 136, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 163, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Name_Of_Column_I_Search_Trough'

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

Traceback (most recent call last):
  File "C:\XXX\main.py", line 6, in <module>
    print(df.loc[df['Name_Of_Column_I_Search_Trough'] == 'Known_Item_Number'])
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3505, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3623, in get_loc
    raise KeyError(key) from err
KeyError: 'Name_Of_Column_I_Search_Trough'

If I choose differently



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source