I have a number of pandas.Dataframe
objects and want to reorder the columns of all of them in a for
loop, but it's not working. What I have is:
import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.rand(5, 5))
df2 = pd.DataFrame(np.random.rand(5, 5))
dfs = [ df1, df2 ]
Now, changing the name of the columns works:
for df in dfs:
df.columns = [ 'a', 'b', 'c', 'd', 'e' ]
df1.head()
prints (columns with letters instead of numbers):
a b c d e
0 0.276383 0.655995 0.512101 0.793673 0.165763
1 0.841603 0.831268 0.776274 0.670846 0.847065
2 0.626632 0.448145 0.184613 0.763160 0.337947
3 0.502062 0.881765 0.154048 0.908834 0.669257
4 0.254717 0.538606 0.677790 0.088452 0.014447
However, changing the order of the columns is not working in the same way. The following loop:
for df in dfs:
df = df[ [ 'e', 'd', 'c', 'b', 'a' ] ]
leaves the dataframes unchanged.
If I do it for each dataframe, outside the for loop, it works, though:
df1 = df1[ [ 'e', 'd', 'c', 'b', 'a' ] ]
df1.head()
prints the following:
e d c b a
0 0.165763 0.793673 0.512101 0.655995 0.276383
1 0.847065 0.670846 0.776274 0.831268 0.841603
2 0.337947 0.763160 0.184613 0.448145 0.626632
3 0.669257 0.908834 0.154048 0.881765 0.502062
4 0.014447 0.088452 0.677790 0.538606 0.254717
Why can't I loop over the dataframes to change the column order?
How can I loop over the dataframes in the list to change the column order?
Working with python 3.5.3, pandas 0.23.3