Search code examples
pythonpandasdataframecomparisoncryptocurrency

Length of Values Not Matching Length of Index


I am doing some cryptocurrency analysis utilizing the Pandas library for Python. I have produced the following dataframe:

        coin    old_price      current_price
0     BNBBTC    0.000949            0.000994
1     BNBETH    0.011472            0.012129
2    BNBUSDT   10.938950            9.358000
3     VENBNB    0.619480            0.635200

Then, I am attempting to compare the two columns old_price and current_price.

Upon using this line of code below:

comparison['sell'] = np.where((comparison['current_price'] >= comparison['old_price']))

I am receive an error stating:

"ValueError: Length of values does not match length of index"

To the best of my knowledge, the dataframe has the same number of data for each column. Please advise, it would be much appreciated.


Solution

  • np.where(condition) without the second and third optional arguments returns the array of row indexes for which the condition is True. This array in general is shorter than the original DataFrame (in your case, it has only one value):

    np.where(comparison['current_price'] >= comparison['old_price'])
    #(array([2]),)
    

    What you need is probably this:

    comparison['sell'] = (comparison['current_price'] >= comparison['old_price'])
    #array([False, False,  True, False], dtype=bool)