【问题标题】:How to Index A Search Based Off More Than One Column Using Pandas如何使用 Pandas 对基于多列的搜索进行索引
【发布时间】:2020-12-11 07:11:37
【问题描述】:

我在索引用户输入以搜索多个列时遇到问题。这是我的代码

Searched_Multicast_Row_Location = excel_data_df_Sheet_1[excel_data_df_Sheet_1['Zixi Multicast'] == Group.get()].index
print(Searched_Multicast_Row_Location)

这很好用,但问题是,用户可能会输入不同列中的值,我也想索引它。我尝试了以下

Searched_Multicast_Row_Location = excel_data_df_Sheet_1[excel_data_df_Sheet_1['Zixi Multicast','Gateway Card Multicast'] == Group.get()].index
print(Searched_Multicast_Row_Location)

我希望我可以将任一索引存储到单个 var 中

我收到以下错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\206415779\Anaconda3\envs\FINDIT\lib\site-packages\pandas\core\indexes\base.py", line 2889, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 97, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1675, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1683, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: ('Zixi Multicast', 'Gateway Card Multicast')

上述异常是以下异常的直接原因:

Traceback (most recent call last):
File "C:\Users\206415779\Anaconda3\envs\FINDIT\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:/Users/206415779/Python/FINDIT/FINDIT START", line 221, in Okay
Searched_Multicast_Row_Location = excel_data_df_Sheet_1[excel_data_df_Sheet_1['Zixi Multicast','Gateway Card Multicast'] == Group.get()].index
File "C:\Users\206415779\Anaconda3\envs\FINDIT\lib\site-packages\pandas\core\frame.py", line 2899, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Users\206415779\Anaconda3\envs\FINDIT\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
raise KeyError(key) from err
**KeyError: ('Zixi Multicast', 'Gateway Card Multicast')**

【问题讨论】:

  • 感谢@David Erickson 的编辑,现在我知道错误是否应该出现在代码括号中。
  • 四个空格,或者我所做的是将错误复制并粘贴到我的控制台中,突出显示所有代码并按 Tab。然后,将其粘贴回 StackOverflow。
  • @David Erickson 感谢您的帮助!
  • 我已经尝试了以下Searched_Multicast_Row_Location = excel_data_df_Sheet_1[excel_data_df_Sheet_1[("IPX Multicast") or ("Gateway Card Multicast")] == Group.get()].index print(Searched_Multicast_Row_Location) 问题是,它现在只搜索后一个标题“网关卡多播”,当我在标题“IPX 多播”下搜索一个值时,它确实不索引该行,我只剩下 Null 数据。

标签: python excel pandas tkinter


【解决方案1】:

这就是我所需要的。这会从“Group.get()”搜索用户输入并查询多个列,然后它索引行#,以便我可以从该特定行中获取数据。希望这对未来的人有所帮助。从一些发现看来,新版本的 pandas 不支持“或”操作,所以你应该使用 |而是。

Searched_Multicast_Row_Location = excel_data_df_Sheet_1[excel_data_df_Sheet_1['Zixi Multicast'] == Group.get()].index | excel_data_df_Sheet_1[excel_data_df_Sheet_1['Gateway Card Multicast'] == Group.get()].index
print(Searched_Multicast_Row_Location)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2017-07-27
    • 2014-11-29
    • 1970-01-01
    • 2019-02-08
    • 2020-11-16
    • 1970-01-01
    相关资源
    最近更新 更多