Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas reorder columns

# 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()
Comment

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
Comment

pandas reorder columns

# Get column list in ['item1','item2','item3'] format 
df.columns
# [0]output: 
Index(['item1','item2','item3'], dtype='object')

# Copy just the list portion of the output and rearrange the columns 
cols = ['item3','item1','item2']

# Resave dataframe using new column order
df = df[cols]
Comment

reorder columns pandas

cols = df.columns.tolist()
# Rearrange the list any way you want
cols = cols[-1:] + cols[:-1]
df = df[cols]
Comment

change dataframe columns order

df = df[['col1', 'col2', 'col3', 'col4']]
Comment

rearrange columns 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']
Comment

how to reorder columns in pandas

Reorder columns in pandas
Comment

PREVIOUS NEXT
Code Example
Python :: pycountry 
Python :: create pyspark dataframe from list 
Python :: python pandas get labels 
Python :: converting numpy array to dataframe 
Python :: python last n list elements 
Python :: python to uppercase 
Python :: loop through words in a string python 
Python :: np.select with multiple conditions 
Python :: round tuple 
Python :: python pandas read csv from txt tab delimiter 
Python :: how to concatenate dataframe in python 
Python :: python remove blanks from string 
Python :: django print query 
Python :: numpy array unique value counts 
Python :: How to Use Python all() Function to Check for Letters in a String using all function 
Python :: huggingface dataset from pandas 
Python :: pandas get outliers 
Python :: python planet list 
Python :: plt get colors in range 
Python :: Python program to print negative numbers in a list 
Python :: click a button using selenium python 
Python :: select all rows in a table flask_ sqlalchemy (python) 
Python :: python async function 
Python :: python request coinmarketcap 
Python :: how to use random tree in python 
Python :: django get query parameters 
Python :: check if something is nan python 
Python :: how to use pip commands in pycharm 
Python :: python print f 
Python :: multiprocessing join python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =