Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

insert row at given position in pandas dataframe

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)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #insert #row #position #pandas #dataframe
ADD COMMENT
Topic
Name
2+4 =