Ya no necesitas investigar más por internet ya que estás al lugar perfecto, contamos con la solución que buscas y sin complicarte.
Ejemplo 1: Python cambia el orden de las columnas en el marco de datos
cols = df.columns.tolist()
cols = cols[-1:]+ cols[:-1]#bring last element to 1st position
df = df.reindex(cols, axis=1)
Ejemplo 2: los pandas reordenan las columnas
# setting up a dummy dataframe
raw_data ='name':['Willard Morris','Al Jennings','Omar Mullins','Spencer McDaniel'],'age':[20,19,22,21],'favorite_color':['blue','red','yellow',"green"],'grade':[88,92,95,70]
df = pd.DataFrame(raw_data, index =['Willard Morris','Al Jennings','Omar Mullins','Spencer McDaniel'])
df
#now 'age' will appear at the end of our df
df = df[['favorite_color','grade','name','age']]
df.head()
Ejemplo 3: reordenar columnas pandas
cols = df.columns.tolist()# Rearrange the list any way you want
cols = cols[-1:]+ cols[:-1]
df = df[cols]
Ejemplo 4: los pandas reordenan las columnas
# Get column list in ['item1','item2','item3'] format
df.columns
# [0]output:
Index(['item1','item2','item3'], dtype='object')# Copy just the list protion of the output and rearrange the columns
cols =['item3','item1','item2']# Resave dataframe using new column order
df = df[cols]
Ejemplo 5: reorganizar columnas pandas
You could also do something like this:
df = df[['mean','0','1','2','3']]
You can get the list of columns with:
cols =list(df.columns.values)
The output will produce:['0','1','2','3','mean']
Ejemplo 6: los pandas de Python cambian el orden de las columnas
In [39]: df
Out[39]:01234 mean
00.1727420.9156610.0433870.7128330.190717110.1281860.4247710.5907790.7710800.617472120.1257090.0858940.9897980.8294910.155563130.7425780.1040610.2997080.6167510.951802140.7211180.5281560.4213600.1058860.322311150.9008780.0820470.2246560.1951620.736652160.8978320.5581080.3180160.5865630.507564170.0271780.3751830.9302480.9217860.337060180.7630280.1829050.9317560.1106750.423398190.8489960.3105620.1408730.3045610.4178081
In [40]: df = df[['mean',4,3,2,1]]
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)