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 :: directory name python 
Python :: how to add a list to dataframe in python 
Python :: how to add contents of one dict to another in python 
Python :: install python package from git colab 
Python :: python class tostring 
Python :: middle value of a list in python 
Python :: Why do we use graphs? 
Python :: migrate using other database django 
Python :: python get pixel color 
Python :: python wsgi server 
Python :: python how to get directory of script 
Python :: python matplotlib hist set axis range 
Python :: how to find shortest string in a list python 
Python :: 1052 uri solution 
Python :: python opencv open camera 
Python :: embed_author discord.py 
Python :: identify null values 
Python :: how to reapete the code in python 
Python :: pandas to tensor torch 
Python :: mouse module python 
Python :: discord python wait for user input 
Python :: django queryset group by sum 
Python :: how to get width of an object in pyqt5 
Python :: django admin image 
Python :: what is the purpose of the judiciary 
Python :: MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory 
Python :: sklearn rmse 
Python :: use python type hint for multiple return values 
Python :: python image plot 
Python :: python get files in directory 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =