How to invert a column in Python?

0

I would like to know how to flip the "Name, Last, Age" to "Age, First, Last"

Followmycode:

importpandasaspd#Importaabiblioteca"Python Data Analysis"

df = pd.read_excel('Pasta1.xlsx') #Lê o arquivo xlsx

df = df.drop('MiddleInitial', axis = 1) #Remove a coluna "MiddleInitial"
df = df.drop('Gender', axis = 1) #Remove a coluna "Gender"
df.rename(columns = {"Age": "Idade", "GivenName": "Nome", "Surname": "Sobrenome"}, inplace = True) #Altera o nome das colunas
df.head() #lê a linha a cima


dfOrdenado = df.sort_values(by = 'Nome', ascending = True) #Ordena a lista
print(dfOrdenado) #Imprime a Lista
    
asked by anonymous 17.04.2018 / 23:58

2 answers

1

You can change the order of the columns by passing the list with the columns to the dataframe. In the same way that you can change the order, you can also select fewer columns if needed.

dfOrdenado[['Idade', 'Nome', 'Sobrenome']]
    
18.04.2018 / 01:51
0

Enter the following code:

dfOrdenado[['Idade','Nome','Sobrenome']]

It will sort by column name

    
18.04.2018 / 02:16