0

I've got a dataframe with non-standard columns (not the same info all the way down). I want to search for a particular phrase that may or may not appear in some column of a row and then return the value of the succeeding cell. For example:

A               B               C
How many?       5               Blah blah
Blah            How many?       7
How many?       13              Blah
Blah            Blah            Blah

I'm trying to figure out how to search for the phrase "How many?" and then return the value in the next cell (5/7/13/null)

2
  • 1
    What do you mean by "the next cell"? One cell to the right? Commented Nov 8, 2018 at 15:19
  • Yeah, I'm basically trying to search for a phrase and if it's found, then return that cell and the cell to the right of it (which is always the "value" in my data set). There is a lot of garbage in my data set so want to essentially just return those 2 items and dump everything else in the row. Commented Nov 8, 2018 at 15:25

2 Answers 2

2

With Boolean and shift

df[df.eq('How many?').shift(1,axis=1).fillna(False)]
Out[142]: 
     A    B    C
0  NaN    5  NaN
1  NaN  NaN    7
2  NaN   13  NaN
3  NaN  NaN  NaN

Update

s1=df.eq('How many?').shift(1,axis=1).fillna(False)
s2=df.eq('How many?')
df[s1|s2]
Out[154]: 
          A          B    C
0  How many?         5  NaN
1       NaN  How many?    7
2  How many?        13  NaN
3       NaN        NaN  NaN
Sign up to request clarification or add additional context in comments.

2 Comments

@Alex yep , fixed
This is perfect, thanks! Still fairly new to python/pandas and didn't know about shift. I assume the 1 when you call shift is to return the cell 1 to the right? Is there an easy way to return both the cell with the search term and that one?
1

Use numpy array instead for easier indexing:

mask = df.values == 'How many?'
your_list = [df.values[i, j+1] for i, j in zip(*np.where(mask)) if j < df.values.shape[1]-1]
# yourlist = ['5', '7', '13']

1 Comment

@Alex fixed. Missed a * operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.