NaN when subtracting pandas data frames

I have two data frames with several overlapping indexes and columns.

old = pd.DataFrame(index = ['A', 'B', 'C'], columns = ['k', 'l', 'm'], data = abs(np.floor(np.random.rand(3, 3)*10))) new = pd.DataFrame(index = ['A', 'B', 'C', 'D'], columns = ['k', 'l', 'm', 'n'], data = abs(np.floor(np.random.rand(4, 4)*10))) 

I want to calculate the difference between them and try

 delta = new - old 

This gives a lot of NaN where the indices and columns do not match. I would like to treat the absence of indexes and columns as zero, (old ['n', 'D'] = 0). old will always be a subspace of the new.

Any ideas?

EDIT: I guess I didn't explain it enough. I do not want to populate a delta frame with zeros. I want to handle the missing indexes and columns old as if they were zeros. Then I got the value in the new ['n', 'D'] in delta instead of NaN.

+6
source share
1 answer

Use sub with fill_value=0 :

 In [15]: old = pd.DataFrame(index = ['A', 'B', 'C'], columns = ['k', 'l', 'm'], data = abs(np.floor(np.random.rand(3, 3)*10)))​ new = pd.DataFrame(index = ['A', 'B', 'C', 'D'], columns = ['k', 'l', 'm', 'n'], data = abs(np.floor(np.random.rand(4, 4)*10))) delta = new.sub(old, fill_value=0) delta Out[15]: klmn A 0 3 -9 7 B 0 -2 1 8 C -4 1 1 7 D 8 6 0 6 
+5
source

Source: https://habr.com/ru/post/1012411/


All Articles