Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 216
    In contrast to what cs95 says, there are perfectly fine reasons to want to iterate over a dataframe, so new users should not feel discouraged. One example is if you want to execute some code using the values of each row as input. Also, if your dataframe is reasonably small (e.g. less than 1000 items), performance is not really an issue. Commented Oct 16, 2019 at 8:53
  • 17
    If you are a beginner to this thread and are not familiar with the pandas library, it's worth taking a step back and evaluating whether iteration is indeed the solution to your problem. In some cases, it is. In most cases, it isn't. It is important to introduce beginners to the library by easing them into the concept of vectorization so they know the difference between writing "good code", versus "code that just works" - and also know when to use which. Commented Apr 18, 2023 at 7:15
  • Related: Are for-loops in pandas really bad? When should I care? Commented Oct 5, 2023 at 3:25
  • See also: What is the most efficient way to loop through dataframes with pandas?. Commented Nov 23, 2023 at 21:35