theprowler
theprowler

Reputation: 3610

Pandas - search for strings inside of DataFrame cells

I want to search a DataFrame just like I'd search a txt file: I want to scan thru it and look for occurrences of a word, and if that word appears I want to capture the number that follows it.

Here is fish_frame:

fish_frame:                   0           1           2  ASK            TRADE_DATE
0          Species       Price      Weight    1  2013-06-21 14:07:00   
1          GBW Cod         .55       8,059    1  2013-06-21 14:07:00   
2      GBE Haddock         .03      14,628    1  2013-06-21 14:07:00   
3      GBW Haddock         .02      87,451    1  2013-06-21 14:07:00   
4            GB YT        1.50       1,818    1  2013-06-21 14:07:00   
5            Witch        1.25       1,414    1  2013-06-21 14:07:00   
6        GB Winter         .40      23,757    1  2013-06-21 14:07:00   
7          Redfish         .02         123    1  2013-06-21 14:07:00   
8       White Hake         .40         934    1  2013-06-21 14:07:00   
9          Pollock         .02       7,900    1  2013-06-21 14:07:00   
10  Package Price:         NaN  $21,151.67    1  2013-06-21 14:07:00   
11         Species      Weight        None    1  2013-06-21 14:07:00   
12         GBE Cod         820        None    1  2013-06-21 14:07:00   
13         GBW Cod      15,279        None    1  2013-06-21 14:07:00   
14     GBE Haddock      32,250        None    1  2013-06-21 14:07:00   
15     GBW Haddock     192,793        None    1  2013-06-21 14:07:00   
16           GB YT       6,239        None    1  2013-06-21 14:07:00   
17          SNE YT       2,018        None    1  2013-06-21 14:07:00   
18          GOM YT       1,511        None    1  2013-06-21 14:07:00   
19          Plaice       2,944        None    1  2013-06-21 14:07:00   
20           Witch       1,100        None    1  2013-06-21 14:07:00   
21       GB Winter     158,608        None    1  2013-06-21 14:07:00   
22      White Hake          31        None    1  2013-06-21 14:07:00   
23         Pollock       1,983        None    1  2013-06-21 14:07:00   
24      SNE Winter       7,257        None    1  2013-06-21 14:07:00   
25           Price  $58,500.00        None    1  2013-06-21 14:07:00

So with my DataFrame, I want to look for any occurrence of the word Price and if it appears, then capture the number that follows it. Like on lines 10 and 25.

I tried simple commands such as if 'Price' in row: do this but those didn't work since a DataFrame clearly isn't a txt file. So I tried experimenting with :

for row in fish_frame.iterrows():
    fish_frame.str.split('Price')
    print("fish_frame_split:", fish_frame)

after reading this snippet of code directly from the Pandas website (https://pandas.pydata.org/pandas-docs/stable/text.html).:

In [15]: s2 = pd.Series(['a_b_c', 'c_d_e', np.nan, 'f_g_h'])

In [16]: s2.str.split('_')
Out[16]: 
0    [a, b, c]
1    [c, d, e]
2          NaN
3    [f, g, h]
dtype: object

But it resulted in an error: AttributeError: 'DataFrame' object has no attribute 'str'

So all in all I'm confused about why that command fails for me, how to match the word Price in my DataFrame, and then how to append the number that follows Price to all its corresponding Fish-Weight pairs.

This is as simplified as I could make it, I think, so thanks for any help.

Upvotes: 1

Views: 1027

Answers (1)

shivsn
shivsn

Reputation: 7848

This should do it:

fish_frame[fish_frame[0].str.contains('Price')][1]

Upvotes: 1

Related Questions