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 :: hash() python 
Python :: pandas como eliminar filas con valores no nulos en una columna 
Python :: render django template 
Python :: pyflakes invalid syntax 
Python :: how to create a loading in pyqt5 
Python :: how to install docx in python 
Python :: install older version of python 
Python :: python compiler to exe 
Python :: remove first item form dictionary python 
Python :: numpy mean 
Python :: add a button on tkinter 
Python :: pandas .nlargest 
Python :: import discord python 
Python :: markers seaborn 
Python :: local ip 
Python :: access env variable in flask 
Python :: find frequency of numbers in list python 
Python :: python modulus 
Python :: python convert int to hex string 
Python :: filter dict by list of keys python 
Python :: count specific instances in a columb in pandas 
Python :: discordpy owner only command 
Python :: user information in python 
Python :: python crop string 
Python :: python insert path 
Python :: django serve media folder 
Python :: how to download packages using pip 
Python :: ordenar lista decrescente python 
Python :: increase recursion depth google colab 
Python :: sklearn regression 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =