在行Pandas python上使用部分字符串匹配返回DataFrame项目 [英] Return DataFrame item using partial string match on rows pandas python

查看:207
本文介绍了在行Pandas python上使用部分字符串匹配返回DataFrame项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两列的数据框,其中一列是字符串,另一列是列表,如下所示:

I have a Dataframe of two columns, with strings in one column and lists in the other, as below:

      RSD_TYPE                                FILTER LIST
   0     AQ500          [N/A, Z mean, SNR mean, Dir mean]
   1    Triton  [wipe mean, Z mean, Avail mean, Dir mean]
   2  Windcube            [N/A, W mean, Q mean, Dir mean]
   3    Zephir     [Rain mean, W mean, Packets, dir mean]

我想基于部分字符串与列RSD_TYPE的元素匹配返回一个列表.例如.搜索哪一行与"AQ5"匹配的部分字符串,然后从该行返回相应的列表项,在这种情况下为[N/A,Z均值,SNR均值,Dir均值].

I want to return a list based on partial string match with the elements of the column RSD_TYPE. E.G. search for which row has a partial string match with "AQ5", then return the corresponding list item from that row, in this case [N/A, Z mean, SNR mean, Dir mean].

计划是使用.get_value做到这一点的,但是首先我需要一种使用部分字符串匹配返回(行)索引的方法.那就是我被困住的地方.我知道如何在列标题上运行部分字符串匹配,但是我找不到在该列中的元素(或整个数据框)上运行它的方法.有什么想法吗?

The plan was to do this using .get_value, but first I need to have a way of returning (row) index using a partial string match. That's where I'm stuck. I know how to run a partial string match on column titles but I can't find a way to run it on the elements in that column (or the dataframe as a whole). Any ideas?

非常感谢.

推荐答案

尝试一下:

df[df['RSD_TYPE'].str.contains("AQ5")]['FILTER LIST']

示例:

In [3]: df
Out[3]:
   RSD_TYPE                                FILTER LIST
0     AQ500          [N/A, Z mean, SNR mean, Dir mean]
1    Triton  [wipe mean, Z mean, Avail mean, Dir mean]
2  Windcube            [N/A, W mean, Q mean, Dir mean]
3    Zephir     [Rain mean, W mean, Packets, dir mean]

[4 rows x 2 columns]


In [4]: df[df['RSD_TYPE'].str.contains("AQ5")]
Out[4]:
  RSD_TYPE                        FILTER LIST
0    AQ500  [N/A, Z mean, SNR mean, Dir mean]

[1 rows x 2 columns]


In [5]: df[df['RSD_TYPE'].str.contains("AQ5")]['FILTER LIST']
Out[5]:
0    [N/A, Z mean, SNR mean, Dir mean]
Name: FILTER LIST, dtype: object

这篇关于在行Pandas python上使用部分字符串匹配返回DataFrame项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆