pandas 使用行熊猫 python 上的部分字符串匹配返回 DataFrame 项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22845959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:53:26  来源:igfitidea点击:

Return DataFrame item using partial string match on rows pandas python

pythonstringpandasmatch

提问by vbastrangledpython

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]

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].

我想根据与 RSD_TYPE 列的元素匹配的部分字符串返回一个列表。EG 搜索哪一行有部分字符串与“AQ5”匹配,然后从该行返回相应的列表项,在这种情况下 [N/A, Z mean, SNR mean, Dir mean]。

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?

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

Many thanks in advance.

提前谢谢了。

回答by Amit Verma

Try this:

尝试这个:

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

Example:

例子:

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