如何使用熊猫为基于多列的搜索建立索引

问题描述

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

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)

我希望我可以将其中一个的索引存储到单个变量中

我收到以下错误

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

解决方法

这就是我需要的东西。这将从“ Group.get()”中搜索用户输入并查询多个列,然后索引行号,以便我可以从该特定行中获取数据。希望这可以帮助某人。从一些发现来看,新版本的熊猫似乎不支持“或”操作,因此应使用|。代替。

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)