简体   繁体   中英

Combining two data frames and showing their difference - Python Pandas

I have two dataframes which will be pulled from CSV files:

   X     Value 1  Value 2
0  1     2        1      
1  3     4       -2      
2  3     3        8      
3 -1     4        2      
4  6    -8        2      
5  0     10       1

   X     Value 1  Value 2
0  1     2        1      
1  3     4       -1      
2  3     4        8      
3 -1     4        2      
4  6    -8        2      
5  0     9        1 

I would like to use Python Pandas to have an output like this, where it would be the second data frame but for any values that have changed there will be a plus/minus in brackets of the amount it has changed by:

   X     Value 1  Value 2
0  1     2        1      
1  3     4       -1 (+1)     
2  3     4 (+1)   8      
3 -1     4        2      
4  6    -8        2      
5  0     9 (-1)  1 

The only thing close to a solution I found online made use of Panels with are deprecated so I want to avoid using them. Also I would like the final output to be a dataframe so that I can apply styles to it.

You can use sub with applymap first and last add original df2 converted to string s:

  • if length of both DataFrames is same
  • if values of indexes are same in both DataFrames
  • if columns names are same in both DataFrames

df = df2.sub(df1).applymap(lambda x: ' ({0:+d})'.format(x) if x != 0 else '')
print (df)
  X Value 1 Value 2
0                  
1              (+1)
2      (+1)        
3                  
4                  
5      (-1) 

df3 = df2.astype(str).add(df)
print (df3)
   X Value 1  Value 2
0  1       2        1
1  3       4  -1 (+1)
2  3  4 (+1)        8
3 -1       4        2
4  6      -8        2
5  0  9 (-1)        1

You can get the difference between df2 and df1 and create the + or - flag and then append this to df2.

df2.astype(str) + \
(df2-df1).applymap(lambda x: ' ({}{})'\
                   .format('+' if x > 0 else '', str(x)).replace('(0)',''))

Out[240]: 
     X Value 1  Value 2
0   1       2        1 
1   3       4   -1 (+1)
2   3   4 (+1)       8 
3  -1       4        2 
4   6      -8        2 
5   0   9 (-1)       1     

Without formatting skills :

(df2.astype(str)+"("+(df2-df1).astype(str)+")").applymap(
lambda s:s.replace("(0)","").replace("(","(+").replace("+-","-"))

for :

    X Value1  Value2
0   1      2       1
1   3      4  -1(+1)
2   3  4(+1)       8
3  -1      4       2
4   6     -8       2
5   0  9(-1)       1

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