Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

insert row in any position pandas dataframe

line = DataFrame({"onset": 30.0, "length": 1.3}, index=[3])
df2 = concat([df.iloc[:2], line, df.iloc[2:]]).reset_index(drop=True)
Comment

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)
Comment

add row to dataframe with index

In [99]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])

In [100]: s = df.xs(3)

In [101]: s.name = 10

In [102]: df.append(s)
Out[102]: 
           A         B         C         D
0  -2.083321 -0.153749  0.174436  1.081056
1  -1.026692  1.495850 -0.025245 -0.171046
2   0.072272  1.218376  1.433281  0.747815
3  -0.940552  0.853073 -0.134842 -0.277135
4   0.478302 -0.599752 -0.080577  0.468618
5   2.609004 -1.679299 -1.593016  1.172298
6  -0.201605  0.406925  1.983177  0.012030
7   1.158530 -2.240124  0.851323 -0.240378
10 -0.940552  0.853073 -0.134842 -0.277135
Comment

add row to dataframe with index


In [99]: df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])

In [100]: s = df.xs(3)

In [101]: s.name = 10

In [102]: df.append(s)
Out[102]: 
           A         B         C         D
0  -2.083321 -0.153749  0.174436  1.081056
1  -1.026692  1.495850 -0.025245 -0.171046
2   0.072272  1.218376  1.433281  0.747815
3  -0.940552  0.853073 -0.134842 -0.277135
4   0.478302 -0.599752 -0.080577  0.468618
5   2.609004 -1.679299 -1.593016  1.172298
6  -0.201605  0.406925  1.983177  0.012030
7   1.158530 -2.240124  0.851323 -0.240378
10 -0.940552  0.853073 -0.134842 -0.277135

Comment

PREVIOUS NEXT
Code Example
Python :: Display the data types of the DataFrame 
Python :: merge all mp4 video files into one file python 
Python :: remove leading and lagging spaces dataframe python 
Python :: pandas df to dict 
Python :: How To Get Redirection URL In Python 
Python :: django request user 
Python :: python3 lowercase 
Python :: sharpdevelop pause python code 
Python :: clone keras model 
Python :: change a cell in pandas dataframe 
Python :: tuple and list in python 
Python :: selenium chrome options suppress warnings python 
Python :: python namespace 
Python :: python-telegram-bot send file 
Python :: python get array length 
Python :: plus in python 
Python :: concatenate list of strings 
Python :: get random number positive or negative python 
Python :: python empty constructor 
Python :: random python range 
Python :: load image metadata with pil 
Python :: run python script on android 
Python :: python program to print the fibonacci sequence 
Python :: mulitplication symbo for unpacking in python 
Python :: python try except finally 
Python :: adding number in set in python 
Python :: merge two dictionaries 
Python :: python index max list 
Python :: python cv2 write to video 
Python :: python file write 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =