0

I need to insert multiple new columns in between columns in a dataframe

Input dataframe:

  PC GEO BL RL JanTOTAL  BL RL FebTOTAL
   A USA  1  1        2   1  1        2
   B IND  1  1        2   1  1        2


Expected Output dataframe

PC GEO Jan-Month        BL RL JanTOTAL     Feb-Month        BL RL FebTOTAL 
A  USA  2019-01-01       1  1        2    2019-02-01         1  1       2
B  IND  2019-01-01       1  1        2    2019-02-01         1  1       2
ansev
  • 30,322
  • 5
  • 17
  • 31
Praveen Snowy
  • 163
  • 2
  • 10

1 Answers1

0

You can follow this example:

import pandas as pd
from datetime import datetime
df=pd.DataFrame()
df['a']=[1]
df['b']=[2]
print(df)
df['Jan-Month']=datetime(2019,1,1)
df['Feb-Month']=datetime(2019,2,1)
print(df)
df=df.reindex(columns=['Jan-Month','a','Feb-Month','b'])
print(df)

Output:

   a  b
0  1  2

   a  b  Jan-Month  Feb-Month
0  1  2 2019-01-01 2019-02-01

   Jan-Month  a  Feb-Month  b
0 2019-01-01  1 2019-02-01  2
ansev
  • 30,322
  • 5
  • 17
  • 31
  • if i have 10 rows... will the "Jan-Month" column automatically have 10 rows of data? – Praveen Snowy Aug 19 '19 at 13:20
  • No, you need use a list, example:`df['Jan-Month']=[datetime(2019,1,1),datetime(2019,1,1),datetime(2019,1,2)] ` this is simple – ansev Aug 19 '19 at 14:21