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

乔希

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

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章