Feature Type

  • [x] 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()

EDIT (4/4/25): Alternatively, use map and an anonymous function to create the generator.

map(lambda x: x._asdict(), df.itertuples())

Additional Context

No response

Comment From: Liam3851

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

Comment From: narukaze132

@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.