1

When comparing two dataframes and putting the result back into a dataframe:

dfA = pd.DataFrame({'Column':[1,2,3,4]})

or in human readable form:

    Column
0   1
1   2
2   3
3   4

dfB = pd.DataFrame({'Column':[1,2,4,3]})

or in human readable form:

    Column
0   1
1   2
2   4
3   3

pd.DataFrame(dfA > dfB)

pandas outputs a dataframe with true or false values.

    Column
0   False
1   False
2   False
3   True

Is it possible to change the name from 'true' or 'false' to 'lower' or 'higher'? I want to know if the outcome is higher, lower or equal, that is why I ask. If the output is not higher or lower, (true or false) then it must be equal.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
nappingkid
  • 175
  • 2
  • 3
  • 11

2 Answers2

2

You may use map:

In [10]: pd.DataFrame(dfA > dfB)['Column'].map({True: 'higher', False: 'lower'})
Out[10]:
0     lower
1     lower
2     lower
3    higher
Name: Column, dtype: object
user3483203
  • 50,081
  • 9
  • 65
  • 94
  • This is an interesting approach, would this method be able to work with several columns at the same time. Like when comparing dataframes with multiple columns? + How would I include is neither higher or lower, but equal to this method? – nappingkid Apr 19 '18 at 13:49
  • I add to your solution according to [https://stackoverflow.com/questions/39109045/numpy-where-with-multiple-conditions/39109969]. So, you gave the right answer for my case. But you need to add the ternary solution here. – nappingkid Apr 19 '18 at 13:59
1

I'd recommend np.where for performance/simplicity.

pd.DataFrame(np.where(dfA > dfB, 'higher', 'lower'), columns=['col'])

      col
0   lower
1   lower
2   lower
3  higher

You can also nest conditions if needed with np.where,

m1 = dfA > dfB
m2 = dfA < dfB  
pd.DataFrame(
    np.where(m1, 'higher', np.where(m2, 'lower', 'equal')),
    columns=['col']
)

Or, follow a slightly different approach with np.select.

pd.DataFrame(
    np.select([m1, m2], ['higher', 'lower'], default='equal'),
    columns=['col']
)

      col
0   equal
1   equal
2   lower
3  higher
cs95
  • 379,657
  • 97
  • 704
  • 746
  • I used a ternary as suggested here: [https://stackoverflow.com/questions/39109045/numpy-where-with-multiple-conditions/39109969] – nappingkid Apr 19 '18 at 13:57
  • @nappingkid cut is an interesting solution. The ternery you mention is much like what is in my answer here. – cs95 Apr 19 '18 at 13:58