import pandas as pd import numpy as np x=pd.DataFrame([{'BOY':1,'GIRL':44},{'BOY':22,'GIRL':100}]) print(x) x=x.T #TRANSPOSE IT AND MAKE IT AS COLUMN x.insert(1,2,[44,56]) #INSERT A NEW COLUMN AT ANY POSITION x=x.T # NOW TRANSPOSE IT AGAIN TO MAKE IT ROW AGAIN x=x.reset_index(drop=True) # RESET INDEX print(x)
Here is what the above code is Doing:
1. Create a dataframe with two rows and two columns.
2. Transpose the dataframe to make it a column.
3. Insert a new column at any position.
4. Transpose the dataframe to make it a row again.
5. Reset the index.