Let's say I have a df
of python strings:
string
0 this house has 3 beds inside
1 this is a house with 2 beds in it
2 the house has 4 beds
I want to extract how many beds each house has. I felt a good way to do this would be to just find the item before beds
.
While attempting to complete this problem, I of course noticed strings are indexed by character. That means I would have to turn the strings into a list with str.split(' ')
.
Then, I can find the index of 'beds' in each of the strings, and return the previous index. I tried both a list comprehension and df.iterrows()
for this and can't seem to figure out the right way to do it. My desired output is:
string beds
0 this house has 3 beds inside 3
1 this is a house with 2 beds in it 2
2 the house has 4 beds 4