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

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 :: python backward difference 
Python :: how to edit variables with functions in python 
Python :: how to record the steps of mouse and play the steps using python 
Python :: python find location of module 
Python :: find nth root of m using python 
Python :: get duplicate and remove but keep last in python df 
Python :: iterar una lista en python 
Python :: huggingface default cache dir 
Python :: createview django 
Python :: python format decimal 
Python :: python split string regular expression 
Python :: enumerate in python 
Python :: how to write to a file in python without deleting all content 
Python :: all subarrays of an array python 
Python :: Setting a conditional variable in python. Using an if else statement in python. 
Python :: how to increment date by one in python 
Python :: python print unicode character 
Python :: argparse list 
Python :: mongodb group by having 
Python :: binary search tree iterator python 
Python :: distribution seaborn 
Python :: python requests cookies 
Python :: how to upload file in python tkinter 
Python :: pandas replace zero with blank 
Python :: swapcase 
Python :: simple colours python 
Python :: discord.py get profile picture 
Python :: how to access variable from another function in same class in python 
Python :: one hot encoding numpy 
Python :: how to add value to to interger in python 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =