Skip to content
Advertisement

Python Pandas iterate over rows and access column names

I am trying to iterate over the rows of a Python Pandas dataframe. Within each row of the dataframe, I am trying to to refer to each value along a row by its column name.

Here is what I have:

JavaScript

I used this approach to iterate, but it is only giving me part of the solution – after selecting a row in each iteration, how do I access row elements by their column name?

Here is what I am trying to do:

JavaScript

My understanding is that the row is a Pandas series. But I have no way to index into the Series.

Is it possible to use column names while simultaneously iterating over rows?

Advertisement

Answer

I also like itertuples()

JavaScript

since row is a named tuples, if you meant to access values on each row this should be MUCH faster

speed run :

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement