Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return DataFrame item using partial string match on rows pandas python [duplicate]

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

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?

Many thanks in advance.

like image 311
vbastrangledpython Avatar asked Mar 20 '23 00:03

vbastrangledpython


1 Answers

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
like image 80
Amit Verma Avatar answered Apr 12 '23 11:04

Amit Verma