Greater/less than comparisons between Pandas DataFrames/Series

后端 未结 1 594
失恋的感觉
失恋的感觉 2021-01-18 16:34

How can I perform comparisons between DataFrames and Series? I\'d like to mask elements in a DataFrame/Series that are greater/less than elements in another DataFrame/Series

相关标签:
1条回答
  • 2021-01-18 16:52

    The problem here is that it's interpreting the index as column values to perform the comparison, if you use .gt and pass axis=0 then you get the result you desire:

    In [203]:
    x.gt(x.mean(axis=1), axis=0)
    
    Out[203]:
           a     b
    0  False  True
    1  False  True
    

    You can see what I mean when you perform the comparison with the np array:

    In [205]:
    x > x.mean(axis=1).values
    
    Out[205]:
           a      b
    0  False  False
    1  False   True
    

    here you can see that the default axis for comparison is on the column, resulting in a different result

    0 讨论(0)
提交回复
热议问题