Viewing a single comment thread. View all comments

phb07jm t1_isudd5v wrote

Can someone please explain why the second is preferable? I would always do the first because it's more likely that the position of a column will change than the name.

21

silvershadow t1_isurezr wrote

Change the iloc to a loc and then I would maybe see the argument.

.iloc and .loc explicitly return the original data frame, while [] indexing can in some cases return a copy. Pandas makes no promises on what you get

So depending on what the full expression was the criticism of using [] inducing could make sense. You’d need to see the full context of what OP was writing though.

From the sounds of what they wrote though, this is not the thinking the interviewer was following.

11