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 :: filter query objects by date range in Django? 
Python :: add new keys to a dictionary python 
Python :: how to make an infinite loop python 
Python :: create a blank image numpy 
Python :: web scraping python beautifulsoup 
Python :: how to create a python script to automate software installation? 
Python :: list all files starting with python 
Python :: count list python 
Python :: move items from one list to another python 
Python :: from django.contrib import messages 
Python :: python return specific elements from list 
Python :: python private 
Python :: size of the query process in python BigQuery 
Python :: python to c# 
Python :: python read file from same directory 
Python :: fromkeys in python 
Python :: how to convert into grayscale opencv 
Python :: install python altair 
Python :: drop na dataframe 
Python :: pd df append 
Python :: numpy save multiple arrays 
Python :: kill python process with bash 
Python :: 3d array numpy 
Python :: norm in python 
Python :: python byte string 
Python :: pandas df to mongodb 
Python :: print out a name in python 
Python :: openai python 
Python :: python counting dictionary 
Python :: python for loop get iteration number 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =