I hope you can help me here, this is probably something simple but its driving me crazy trying to figure it out. So I have used pandas to read data from a csv file into an object "data". If I run "print(data)" I get the first section from the command line below. I want to access the name and the price. "print(data.at[i, "Name"])" works great for the Name column, where I is just a loop counter and represents the rows. However I can't access the price. I thought "print(data.at[i, "price"])" would have worked but it throws the error as you can see from the command line read out below. My full code follows the command line read out.
Comand Line read out
Name SKU Regular price
0 30″ Straight Long Ponytail by Exposed Luxury H... NaN NaN
1 30″ Straight Long Ponytail by Exposed Luxury H... NaN £6.95
2 30″ Straight Long Ponytail by Exposed Luxury H... NaN £6.95
3 30″ Straight Long Ponytail by Exposed Luxury H... NaN £6.95
4 30″ Straight Long Ponytail by Exposed Luxury H... NaN £6.95
... ... ... ...
2814 Yankee Candle Large Jar - Fairy Floss (1pc) NaN £10.50
2815 Yankee Candle Large Jar - Midnight Magnolia (1pc) NaN £10.50
2816 Yankee Candle Large Jar - Rose Lemonade (1pc) NaN £10.50
2817 Yankee Candle Large Jar - Tropical Fruit Punch... NaN £10.50
2818 YSL Touche Eclat Illuminating Pen 2.5ml No.1 (... NaN £15.95
[2819 rows x 3 columns] 30″ Straight Long Ponytail by Exposed Luxury Hair (1pc) - 1001 - Light Blonde, 1 Piece Traceback (most recent call last): File "C:\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3629, 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: 'price'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "C:\Users\drwyn\OneDrive\Desktop\Amazon\Scraper\scrape2.py", line 9, in print(data.at[i, "price"]) File "C:\Python310\lib\site-packages\pandas\core\indexing.py", line 2275, in getitem return super().getitem(key) File "C:\Python310\lib\site-packages\pandas\core\indexing.py", line 2226, in getitem return self.obj._get_value(*key, takeable=self._takeable) File "C:\Python310\lib\site-packages\pandas\core\frame.py", line 3615, in _get_value series = self._get_item_cache(col) File "C:\Python310\lib\site-packages\pandas\core\frame.py", line 3931, in _get_item_cache loc = self.columns.get_loc(item) File "C:\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3631, in get_loc raise KeyError(key) from err KeyError: 'price'
My code
import pandas as pd
data = pd.read_csv("data.csv")
print(data)
i = 1
while i < 6:
print(data.at[i, "Name"])
print(data.at[i, "price"])
i = 1
CodePudding user response:
Check data.headers, make sure there are no typos. Maybe it's Price, not price?
It would help if you could share what the csv file looks like.
CodePudding user response:
A KeyError indicates no column called 'price' exists - maybe a space got in the way?
Try calling data.columns to see the exact names of your columns.
CodePudding user response:
I believe the issue is with your column name. It looks like "Regular price" to me.