best practices for 'ValueError: too many values to unpack' When Iterating Through a Pandas DataFrame?
Hey everyone, I'm running into an issue that's driving me crazy. I'm testing a new approach and Can someone help me understand Hey everyone, I'm running into an issue that's driving me crazy..... I've searched everywhere and can't find a clear answer. I'm currently working on a data processing task using Pandas, and I'm working with an scenario when trying to unpack values from a row of my DataFrame. Specifically, I'm working with a `ValueError: too many values to unpack (expected 2)` behavior when I try to use tuple unpacking in a for loop. Here's the code that generates this behavior: ```python import pandas as pd data = { 'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'Los Angeles', 'Chicago'] } df = pd.DataFrame(data) for name, age in df.iterrows(): print(f'{name} is {age} years old.') ``` I expected `df.iterrows()` to return each row as a tuple containing the index and the Series object, but it seems I'm not correctly unpacking the returned values. I've tried changing the loop to `for index, row in df.iterrows():` and accessing the values as `row['name']` and `row['age']`, which works, but I'd like to know why the original code failed and what the correct unpacking should look like. Am I missing something in understanding how `iterrows()` works? I'm using Pandas version 1.3.5. For context: I'm using Python on Ubuntu. Thanks in advance! Am I missing something obvious? I'm developing on Windows 11 with Python. I recently upgraded to Python 3.11. I'd be grateful for any help. Thanks in advance!