1

I am trying to add a column to the left of the dataframe. By default it seems to add to the right. Is there a way to add the columns to the left?

Here is my code:

import pandas as pd
import numpy as np
df = pd.read_csv("/home/Sample Text Files/sample5.csv", delimiter = "\t")
df=pd.DataFrame(df)
df['Creation_DT']=pd.to_datetime('today')
print(df)

Here is the output:

 ID,Name,Age                Creation_DT
0  1233,Maliva,15 2019-07-17 11:11:37.145194

I want the output to be like this:

Creation_DT, ID, Name, Age
[value], [Value], [Value], [Value]
James Davinport
  • 303
  • 7
  • 19

2 Answers2

2

you can add a line of code to rearrange the columns as follows:

df = df[['Creation_DT', 'ID', 'Name', 'Age']]

another option is to insert the column during conversion:

df.insert(loc=0, column='Creation_DT', value=pd.to_datetime('today'))
Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27
2

try df.insert to place column at specific data

df.insert(loc=0, column='Creation_DT', value=pd.to_datetime('today'))
tawab_shakeel
  • 3,701
  • 10
  • 26