Change Order Of Columns In Pandas Dataframes In A Loop
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
Solution 1:
I've spent a while on it, it actually gave me a nice puzzle.
It works this way, because in your first loop you modify the existing objects, but in the second loop you actually create new objects and overwrite the old ones; by that the list dfs
loses its references to df1
and df2
. If you want the code to work in the way that after second loop you'd like to see the changes applied to df1
and df2
, you can only use methods, that operate on the original dataframe and do not require overwriting.
I'm not convinced that my way is the optimal one, but that's what I mean:
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 ]
fordfin dfs:
df.columns = [ 'a', 'b', 'c', 'd', 'e' ]
fordfin dfs:
forcin ['e', 'd', 'c', 'b', 'a']:
df.insert(df.shape[1],c+'_new',df[c])
#df.drop(['e', 'd', 'c', 'b', 'a'], axis=1)
forcin [ 'a', 'b', 'c', 'd', 'e' ]:
del df[c]
df.columns = ['e', 'd', 'c', 'b', 'a']
Then calling df1
prints:
e d c ba00.5508850.8795570.2026260.2188670.26605710.3440120.7670830.1396420.6851410.55938520.2716890.2473220.7496760.9031620.68038930.6436750.3176810.2172230.7761920.66554240.4804410.9818500.5583030.7805690.484447
Post a Comment for "Change Order Of Columns In Pandas Dataframes In A Loop"