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>

<p>You have all almost all your code, just create <code>dictionary</code> or <code>list</code> and iterate over it and you are done.</p>

<pre><code>import pandas as pd
combinations = [['key1', 'key2', 'msg']]
combinations.append(['Texas 1', '222', 'triple two'])
combinations.append(['Texas 1', '555', 'triple five'])

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

for c in combinations:
    df.ix[(df[0] == c[0]) &amp; (df[2] == c[1]), 1] = c[2]
</code></pre>

<p>Output:</p>

<pre><code>         0            1    2    3
0  Texas 1   triple two  222  333
1  Texas 1  triple five  555  666
2  Texas 2          777  888  999
</code></pre>

python pandas