Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add an iterdicts() function #60901

Open
1 of 3 tasks
narukaze132 opened this issue Feb 9, 2025 · 2 comments
Open
1 of 3 tasks

ENH: Add an iterdicts() function #60901

narukaze132 opened this issue Feb 9, 2025 · 2 comments
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@narukaze132
Copy link

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I wish I could iterate through dictionaries of each row's contents in a DataFrame, the same way I could do so with namedtuples through itertuples(). Performing the namedtuple-to-dict conversion process isn't difficult normally, but in some situation (e.g. doing so in a list/set/dict comprehension), it's more convenient to have a generator that does the process automatically for you.

Feature Description

Add a new function to DataFrames, iterdicts, that takes an index argument (equivalent to the same argument in itertuples) and returns each row as a dictionary the same way itertuples does so as a namedtuple. (No need to take a "name" argument, since that's irrelevant for dictionaries.)

def iterdicts(self, index=True):
    for x in self.itertuples(index):
        yield x._asdict()

Alternative Solutions

Write a custom function that does the same thing externally.

def iterdicts(df, index=True):
    for x in df.itertuples(index):
        yield x._asdict()

Additional Context

No response

@narukaze132 narukaze132 added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 9, 2025
@Liam3851
Copy link
Contributor

Liam3851 commented Feb 10, 2025

iterrows return Series instead of dict on each iteration, but Series is dict-like... does that satisfy your use case?

@narukaze132
Copy link
Author

@Liam3851 No, I'm using dict-merging, which Series is incompatible with. Also, iterrows is much slower than itertuples, and the iterdicts function I showed above seemed almost as fast as the latter when I tried it, though I'll admit I didn't perform extensive benchmarking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants