0

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"

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.