如何在dataframe中搜索元素列表

2024-09-28 21:56:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我的意思是我需要所需的输出数据帧和搜索列表中原始数据帧的特定索引?怎么做?用最快的方法

原始数据帧:

   textA  TextB
0       a        zz
1       bbb        zzzzz
2       ccc        zzz

搜索列表:

search_list = ['a','ccc']

我的意思是我需要所需的输出数据帧和搜索列表中原始数据帧的特定索引

所需输出数据帧:

   textA  TextB
0       a        zz
2       ccc        zzz

所需的特定于输出的索引:

specific_indexes [0, 2]

Computation time is most important.


Tags: 数据方法列表search原始数据listbbbccc
1条回答
网友
1楼 · 发布于 2024-09-28 21:56:51

如果需要检查索引使用的任何列:

idx = df.index[df.isin(search_list).any(axis=1)]

df1 = df[df.isin(search_list).any(axis=1)]

通过更好的性能过滤器索引检查一列:

idx = df.index[df['textA'].isin(search_list)]

df1 = df[df['textA'].isin(search_list)]

相关问题 更多 >