6

While changing order of columns of a pandas dataframe couple of techniques are given.

  1. By reassigning the rearranged columns df = df[['d', 'c', 'b', 'a']] as given here

or

  1. By calling DataFrame.reindex(columns=neworderlist) as given here

The question is whether any of these listed techniques creates copies of the dataframe internally? Is any one of the techniques preferred over the other? A vague reference is given in pandas indexing documentation which discourages chained assignments. Does this 'SettingWithCopy' behaviour arise here as well?

Chadwick Robbert
  • 1,026
  • 1
  • 11
  • 23
  • `'SettingWithCopy'` could be tested very simply on your DF, no? – roganjosh Sep 04 '18 at 10:50
  • Good question, there's a [related comment here](https://stackoverflow.com/questions/51486063/what-is-the-big-o-complexity-of-reversing-the-order-of-columns-in-pandas-datafra#comment90155252_51486063), but I think a good answer will flesh it out. – jpp Sep 04 '18 at 10:53
  • By setting `mode.chained_assignment` to `raise` to raise `SettingWithCopyException` exception? I might be wrong. – Chadwick Robbert Sep 04 '18 at 10:54
  • @ChadwickRobbert it's a warning that's more common than we would probably like, so your code would just raise it anyway if you tried it. But I agree with jpp that it's a good question :) – roganjosh Sep 04 '18 at 10:57

0 Answers0