Answers for "pandas python reorder columns"

5

how to change the column order in pandas dataframe

df = df.reindex(columns=column_names)
Posted by: Guest on August-20-2020
2

pandas reorder columns by name

#old df columns
df.columns
Index(['A', 'B', 'C', 'D'],dtype='***')
#new column format that we want to rearange
new_col = ['D','C','B','A'] #list of column name in order that we want

df = df[new_col]
df.columns
Index(['D', 'C', 'B', 'A'],dtype='***')
#new column order
Posted by: Guest on October-23-2020

Python Answers by Framework

Browse Popular Code Answers by Language