Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to move columns in a dataframen in python

df = df[['column1', 'column2','column3']]
Comment

move column in pandas

import pandas as pd
  
  
# define data
data = {'Age': [55, 20, 35, 10], 'Name': ['Rohan', 'Ritik', 'Sangeeta', 'Yogesh'], 
        'Address': ['Lucknow', 'Delhi', 'Haridwar', 'Nainital'], 
        'Phone Number': [123456789, 234567890, 3456789012, 4567890123]}
  
# create dataframe
df = pd.DataFrame(data)
  
# print original dataframe
print("Original Dataframe")
display(df)
  
# shift column 'Name' to first position
first_column = df.pop('Name')
  
# insert column using insert(position,column_name,
# first_column) function
df.insert(0, 'Name', first_column)
  
print()
print("After Shifting column to first position")
display(df)
Comment

move column in pandas dataframe

column_to_move = df.pop("name")

# insert column with insert(location, column_name, column_value)

df.insert(len(df.columns), "name", column_to_move)
Comment

PREVIOUS NEXT
Code Example
Python :: python generate html 
Python :: sympy 
Python :: python byte like to string 
Python :: python merge list of dict into single dict 
Python :: variables and data types in python 
Python :: python track time 
Python :: python datetime floor to hour 
Python :: python how to print something at a specific place 
Python :: try catch python with open 
Python :: all possible combinations in python 
Python :: how to convert float to string in python 
Python :: odoo sorted 
Python :: how to stop thread python 
Python :: pandas insert a list into cell 
Python :: get index of all element in list python 
Python :: if statement in python 
Python :: flask run 
Python :: python mann kendall test 
Python :: map a list to another list python 
Python :: permission denied when converting dataframe to csv 
Python :: matrix diagonal sum leetcode in Python 
Python :: python number of lines in file 
Python :: wap in python to check a number is odd or even 
Python :: python set attribute by string name 
Python :: loads function in json python 
Python :: python char at 
Python :: numpy reshape (n ) to (n 1) 
Python :: replace nan 
Python :: what are test cases in python 
Python :: convert python list to pyspark column 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =