Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a pythonic way to obtain the difference between dataframes?

Given these 2 dataframes:

A = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0], 
              [5, 1, 4, -5, -4], [1, 5, 2, 2, -20], [2, 4, 4, 3, 0], [3, 3, 1, -1, -1], 
              [4, 2, 2, 0, 0], [5, 1, 4, 20, -2]],
             columns=["A", "B", "C", "D", "E"],
             index=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

B = pd.DataFrame([[1, 5, 2, 8, 2], [2, 4, 4, 20, 2], [3, 3, 1, 20, 2], [4, 2, 2, 1, 0]],
             columns=["A", "B", "C", "D", "E"],
             index=[1, 2, 3, 4])

Is there a pythonic way to get C = A - B, and the output be:

    A   B   C    D    E
5   5   1   4   -5   -4
6   1   5   2    2  -20
7   2   4   4    3    0
8   3   3   1   -1   -1
9   4   2   2    0    0
10  5   1   4   20   -2
like image 508
hernanavella Avatar asked Jan 04 '23 07:01

hernanavella


1 Answers

If the index makes sense, you can subset based on the index:

A[~A.index.isin(B.index)]

enter image description here

like image 120
Psidom Avatar answered Jan 06 '23 22:01

Psidom