Reputation: 125
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.
Upvotes: 0
Views: 1441
Reputation: 7295
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.
Upvotes: 2
Reputation: 369
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.
Upvotes: 2
Reputation: 169269
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.
Upvotes: 3