CodexBloom - Programming Q&A Platform

Unexpected IndexError when parsing CSV with Python 3.11 using pandas

πŸ‘€ Views: 0 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-12
pandas csv indexerror Python

I tried several approaches but none seem to work... I'm working on a personal project and I'm stuck on something that should probably be simple. I'm working on a project where I need to process a CSV file using the `pandas` library in Python 3.11. I have a CSV file with a header row and several columns, but when I try to access a specific cell in the DataFrame, I get an `IndexError`. Here’s the relevant part of my code: ```python import pandas as pd # Read the CSV file into a DataFrame file_path = 'data/sample.csv' df = pd.read_csv(file_path) # Trying to access a specific cell value = df.iloc[5, 2] print(value) ``` The CSV structure is as follows: ``` Name,Age,Location Alice,30,New York Bob,25,Los Angeles Charlie,35,Chicago David,40,Houston Eve,28,Miami Frank,32,Seattle ``` When I run my code, I receive the following behavior: ``` IndexError: single positional indexer is out-of-bounds ``` I verified that the CSV file has six rows (including the header), so I expected to access the value at index `[5, 2]`. I have also tried using `df.shape` to check the dimensions of the DataFrame, which returns `(6, 3)`. I’m not sure why I’m receiving this behavior. I’ve also tried using `df.loc` instead of `df.iloc`, but I encounter the same scenario. Any insights on what might be going wrong or how to troubleshoot this further would be greatly appreciated. Thanks! Am I missing something obvious? I'm working on a CLI tool that needs to handle this. Any ideas what could be causing this? Is there a better approach?