简体   繁体   中英

How to insert a row in DataFrame in pandas? (Python)

Say I have the following dataframe:

a=np.array([[10,20,30,40],[5,20,35,50],[0,5,10,15]])
b=pd.DataFrame(a, columns=["n1", "n2", "n3", "n4"])

I do the following calculation on it (difference between 1st and 2nd row):

c=b.diff(1,0)
c.loc[[1]]

And then I want to insert this row of differences (given by c.loc[[1]] ) into my dataframe b . How do I do that? When I try, I get an error ValueError: cannot set a row with mismatched columns"

Use setting with enlargement by Series selected by loc with one [] :

b.loc[len(b)] = b.diff().loc[1]
print (b)
     n1    n2    n3    n4
0  10.0  20.0  30.0  40.0
1   5.0  20.0  35.0  50.0
2   0.0   5.0  10.0  15.0
3  -5.0   0.0   5.0  10.0

Or use concat with DataFrame with [[]] :

c = pd.concat([b, b.diff().loc[[1]]], ignore_index=True)
print (c)

     n1    n2    n3    n4
0  10.0  20.0  30.0  40.0
1   5.0  20.0  35.0  50.0
2   0.0   5.0  10.0  15.0
3  -5.0   0.0   5.0  10.0

Try

c = b.diff(axis=0).iloc[1]

or

c = b.diff(axis=0).loc[1]

The output:

print(c)
    n1    -5.0
    n2     0.0
    n3     5.0
    n4    10.0
    Name: 1, dtype: float64

to add c to b use append

b = b.append(c)

output of print(b)

     n1    n2    n3    n4
0  10.0  20.0  30.0  40.0
1   5.0  20.0  35.0  50.0
2   0.0   5.0  10.0  15.0
1  -5.0   0.0   5.0  10.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM