Search for a combination in dataframe to change cell value
<p>I want to replace values in a column if the a combination of values in two columns is valid. Lets say I have the following <code>DataFrame</code></p>

<pre><code>df = pd.DataFrame([
        ['Texas 1', '111', '222', '333'],
        ['Texas 1', '444', '555', '666'],
        ['Texas 2', '777','888','999']
    ])

         0    1    2    3
0  Texas 1  111  222  333
1  Texas 1  444  555  666
2  Texas 2  777  888  999
</code></pre>

<p>And if I want to replace the value in <code>column 2</code> if <code>column 0 = Texas 1</code> and the value of <code>column 2 = 222</code>I'm doing the following:</p>

<pre><code>df.ix[ (df.Column 0=='Texas 1')&amp;(df.Column 2 =='222'),Column 2] = "Success" 
</code></pre>

<p>That works fine for a few combinations. The part where I'm lost is how to do this for over 300 combinations? I thought maybe I could use a <code>dict</code> and store the key, which would be <code>'Success'</code> or whatever other value. And the list could be the combination. Kind of like this. </p>

<pre><code>a["Success"] = [Texas 1, 222]
&gt;&gt;&gt; a
{"Success": [Texas 1, 222]}
</code></pre>

<p>But I'm not sure how to do that in a <code>DataFrame</code>.</p>

<h1>Great Use Case for <code>DataFrame.apply()</code>. Lamda functions all the way!!</h1>

<pre><code>df = pd.DataFrame([
        ['Texas 1', 111, 222, 333],
        ['Texas 1', 444, 555, 666],
        ['Texas 2', 777,888,999]
    ])


val_dict = {}
# assumption
# str_like_Success : [column_0 , column_1]
val_dict["Success"] = ['Texas 1', 222]
val_dict["Failure"] = ['Texas 2', 888]
</code></pre>

<p>The function <code>fill_values_from_dict</code> will be applied to each row, where <code>x</code> is the row (Series) and <code>val_dict</code> is the dictionary created above</p>

<pre><code>  def fill_values_from_dict(x,val_dict):
        for key,val in val_dict.items():
            if x[0] == val[0] and x[2] == val[1]:
                x.set_value(1,key)
                return x
        return x
</code></pre>

<p>Apply <code>fill_values_from_dict</code> to each row </p>

<pre><code>df1 = df.apply(lambda x : fill_values_from_dict(x,val_dict),axis=1)
</code></pre>

<p>Output: </p>

<pre><code>   print(df1)

             0        1    2    3
    0  Texas 1  Success  222  333
    1  Texas 1      444  555  666
    2  Texas 2  Failure  888  999
</code></pre>

python pandas