[Python] KeyError: 'False: boolean label can not be used without a boolean index' in Pandas...

Discussão em 'Python' iniciado por Stack, Junho 2, 2025.

  1. Stack

    Stack Membro Participativo

    the order is "Ask the user to enter the name of the object for which he wants to receive data and the names of the filters, data in which you need (it is possible to introduce several filters)."

    here is my data:

    |Object | HJD 24... | Filter | Magnitude|
    |-------|-------------|-----------|----------|
    |SU_Hor | 55896.30476 | B |14.877 |
    SU_Hor 55896.27438 Ic 13.885
    SU_Hor 55896.27349 B 14.809
    SU_Hor 55896.27397 V 14.434
    SU_Hor 55896.40882 Ic 14.033
    SU_Hor 55896.40829 V 14.540
    SU_Hor 55896.40770 B 14.941
    SU_Hor 55896.34973 Ic 13.958
    SU_Hor 55896.34943 V 14.494
    SU_Hor 55896.34906 B 14.861
    SU_Hor 55896.30542 Ic 13.912
    SU_Hor 55896.30512 v 14.440
    SU_Hor 55897.38547 V 14.536
    SU_Hor 55897.28281 B 14.882
    SU_Hor 55897.28317 V 14.428
    SU_Hor 55897.28347 Ic 13.927
    RZ_Lyr 27359.3030 V 10.630
    RZ_Lyr 27684.4510 V 10.610
    RZ_Lyr 27685.4780 V 10.580
    RZ_Lyr 27701.3150 V 10.700
    RZ Lyr 27934.4560 V 10.660
    RZ Lyr 27955.4100 V 10.570
    rzlyr 30604.2000 V 11.030
    RZ_Lyr 55314.5695 B 12.047
    RZ_Lyr 55314.5724 B 12.036
    RZ_Lyr 55314.5900 B 12.042
    RZ_Lyr 55314.6105 B 12.045
    RZ_Lyr 55314.6163 B 12.027
    RZ_Lyr 55342.3509 B 12.057
    RZLyr 55342.3557 B 12.058
    RZ_Lyr 55342.3606 B 12.052
    RZ_Lyr 55342.3654 B 12.058


    this is my code:

    def searchByfilter():
    filter = input('Enter filter to show data \n')
    df = pd.read_csv('Python_2ndLab.csv')
    print(df.loc[df.filter == filter, :])

    print('Enter 1 to search by object name')
    print('Enter 2 to search by filter')

    src = (input('Enter here: '))

    if src == '1':
    searchByObject()
    elif src == '2':
    searchByfilter()
    else:
    print('Sorry, invalid input')


    I'm trying to solve but still didn't work and I got an error:

    Enter 2 to search by filter
    Enter here: 2
    Enter filter to show data
    B
    Traceback (most recent call last):
    File "C:\Users\Acer\PycharmProjects\2nd Lab\main.py", line 78, in <module>
    searchByfilter()
    File "C:\Users\Acer\PycharmProjects\2nd Lab\main.py", line 64, in searchByfilter
    print(df.loc[df.filter == filter, :])
    File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1067, in __getitem__
    return self._getitem_tuple(key)
    File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1247, in _getitem_tuple
    return self._getitem_lowerdim(tup)
    File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 967, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
    File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1311, in _getitem_axis
    self._validate_key(key, axis)
    File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py", line 1118, in _validate_key
    raise KeyError(
    KeyError: 'False: boolean label can not be used without a boolean index'


    I expected to receive the data from all of the "B" filter when I enter "B"

    Continue reading...

Compartilhe esta Página