Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

Add a row at a specific index pandas

df.loc[len(df.index)] = [value1, value2, value3, ...]
Comment

PREVIOUS NEXT
Code Example
Python :: python class with optional arguments 
Python :: Flask / Python. Get mimetype from uploaded file 
Python :: python logging levels 
Python :: webex teams api attach file 
Python :: random list 
Python :: deepcopy python 
Python :: how to stop python for some time in python 
Python :: remove items from list while iterating python 
Python :: pytest snapshot update 
Python :: python serial COM3 
Python :: truncate spaces in python 
Python :: how to encode a string in python 
Python :: Python get the name of the song that is playing 
Python :: number string array 
Python :: pd calculations between columns 
Python :: python bug 
Python :: horizontal barplot 
Python :: python for loop float increment 
Python :: re.search 
Python :: 2d array python 
Python :: python how to add a new key to a dictionary 
Python :: join two strings python 
Python :: speech to text 
Python :: pandas.core.indexes into list 
Python :: how to make a static variable in python 
Python :: class chain methods python 
Python :: how to get the top 100 frequent words on a python dataframe colummn 
Python :: python collection 
Python :: importing python modules from a folder 
Python :: element tree directory python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =