0

I'm picking up someone's code. I'm used to writing for loops. But don't understand the purpose of the underscore in this code.

for _, row in dfDD.iterrows():
   row = row.dropna() #drop empty cols
   if len(row)==1:
       continue #because no operation is defined

   frow = row.filter(like=agflag)
   if not frow.empty:
       *code continues*

I looked at the responses provided here:What is the purpose of the single underscore "_" variable in Python? and here:Underscore _ as variable name in Python

In the above case row is already a variable that's being used, so then what's the need for a throwaway variable?

Have removed the rest of the function code to keep things simple, but can provide if needed. Thanks.

SModi
  • 125
  • 14

3 Answers3

3

iterrows() doesn't iterate over only rows, it

Iterate[s] over DataFrame rows as (index, Series) pairs.

There's no other succinct syntax for grabbing the 2nd (or nth) element of each tuple in an iterable of tuples, so it's customary to unpack these in the for loop and throw the unnecessary data into a _ throwaway variable.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • What is the purpose of iterrows if one is ignoring the index. Why not just use code it as for rows in dfDD? – SModi Sep 28 '20 at 12:17
2

Dataframe.iterrows() iterates over tuples (index, row). The index is not used in this case, thus it is ignored.

Try to give the variable a name and set a breakpoint to see what's inside.

Telcrome
  • 361
  • 2
  • 14
2

Underscore is the same valid variable name as any other name like item, x, username, etc. But it's more like a convention between programmers to throw unused data into the variable with the meaningless name, which indicates that the data in it is also meaningless in a particular context and doesn't matter.

Such a convention of _ variable name usage could be also seen in unpacking (destructuring) code, for example:

items = [1, 2, 3, 4]
first, *_, last = items

This gives you 1, and 4 as first and last, and everything in between is stored into a meaningless list of [2, 3]. The reader of such code understands that those items in _ don't matter in a given context.

Eugene Naydenov
  • 7,165
  • 2
  • 25
  • 43