PYTHON
how to move columns in a dataframen in python
df = df[['column1', 'column2','column3']]
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)
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)
how to move columns in a dataframen in python
df = df[['column1', 'column2','column3']]
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)
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)