簡體   English   中英

KeyError:'Filter'(從數據幀接收特定數據)

[英]KeyError: 'Filter' (receiving specific data from dataframe)

因此,命令是“要求用戶輸入他想要為其接收數據的對象的名稱和過濾器的名稱,以及您需要的數據(可以引入多個過濾器)。”

這是我的數據:

目的 滬京東24... 篩選 震級
SU_Hor 55896.30476 14.877
SU_Hor 55896.27438 我知道了 13.885
SU_Hor 55896.27349 14.809
SU_Hor 55896.27397 V 14.434
SU_Hor 55896.40882 我知道了 14.033
SU_Hor 55896.40829 V 14.540
SU_Hor 55896.40770 14.941
SU_Hor 55896.34973 我知道了 13.958
SU_Hor 55896.34943 V 14.494
SU_Hor 55896.34906 14.861
SU_Hor 55896.30542 我知道了 13.912
SU_Hor 55896.30512 v 14.440
SU_Hor 55897.38547 V 14.536
SU_Hor 55897.28281 14.882
SU_Hor 55897.28317 V 14.428
SU_Hor 55897.28347 我知道了 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天琴座 27934.4560 V 10.660
RZ天琴座 27955.4100 V 10.570
回復 30604.2000 V 11.030
RZ_Lyr 55314.5695 12.047
RZ_Lyr 55314.5724 12.036
RZ_Lyr 55314.5900 12.042
RZ_Lyr 55314.6105 12.045
RZ_Lyr 55314.6163 12.027
RZ_Lyr 55342.3509 12.057
RZLyr 55342.3557 12.058
RZ_Lyr 55342.3606 12.052
RZ_Lyr 55342.3654 12.058

這是我的代碼:

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')

因此,在輸出中,我輸入“2”以按過濾器搜索,然后輸出要求輸入過濾器名稱,然后我輸入“B”,但我收到的不是來自 B 過濾器的數據,而是錯誤。

Name: Filter, dtype: object
Enter 1 to search by object name
Enter 2 to search by filter
Enter here: 2
Enter filter to show data 
B

這是錯誤:

Traceback (most recent call last):
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Filter'

The above exception was the direct cause of the following exception:

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\frame.py", line 3805, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    raise KeyError(key) from err
KeyError: 'Filter'

誰能幫我調試一下? 我真的不知道錯誤在哪里。

我在下面運行了你的代碼,一切都對我有用

import pandas as pd

def searchByFilter():
    filter = input('Enter filter to show data \n')
    df = pd.DataFrame({"Dummy":[1,2,3,4,5],
                       "Filter":["A","B","C","D","E"]})
    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':
    None
elif src == '2':
    searchByFilter()
else:
    print('Sorry, invalid input')

-----------------------------------

Enter 1 to search by object name
Enter 2 to search by filter
Enter here: 2
Enter filter to show data 
B
   Dummy Filter
1      2      B

關鍵錯誤表明您的“Python_2ndLab.csv”文件標題的格式可能與預期不完全一樣。 再次檢查文件並確保沒有任何可能導致此問題的前導或尾隨空格。 在文本編輯器而不是 excel 中打開 CSV 可以更容易地發現這些內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM