Reputation: 175
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.
Upvotes: 1
Views: 202
Reputation: 402303
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
Upvotes: 1
Reputation: 51165
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
Upvotes: 2