0

I want to order the columns of a bunch of dataframes to get a particular column first, thus

for rr in [df1,df2,df3]:
    cols = ["Time"]+[col for col in rr if (col != "Time")]
    rr = rr[cols]

but this fails, apparently, because rr is a copy of each df, so the column order gets changed on rr, but it is not changing the order of the columns of df1, df2, etc.

Tricky...

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
Tunneller
  • 381
  • 2
  • 13
  • initialize a blank list `l=[]` and append `l.append(rr)` after you do `rr = rr[cols]` ? – anky Apr 01 '21 at 17:47
  • I found something similar in https://stackoverflow.com/questions/48536802/iterating-over-different-data-frames-using-an-iterator Bascially create a dict with keys "df1", "df2" and "df3" and load those into the dict as eval(key)... and then an iteration over keys MyFrames[rr] = MyFrames[rr][cols] will create new dataframes and I'll be able to find them later. – Tunneller Apr 01 '21 at 19:01

1 Answers1

1

[Edit]

Following @Jon Clements clever comment, you could even do:

for df in [df1,df2,df3]:
    df.insert(0, 'Time', df.pop('Time'))

[Initial answer]

You could do something like this:

for df in [df1,df2,df3]:
    save = df['Time']
    df = df.drop(columns='Time')
    df.insert(0, 'Time', save)
Laurent
  • 12,287
  • 7
  • 21
  • 37