Changing values in column selectively. Python Pandas
<p>for example i have a fruit dataset that contains the name and colour. How do i change the values in colour column based on the fruit name that i select?</p>

<pre><code>       Name          Color
       Apple         NaN
       Pear          Green
       Pear          Green
       Banana        Yellow
       Watermelon    Green
</code></pre>

<p>I have a rough idea but i have no idea how to code it.</p>

<pre><code>     df[Name] == Apple then df[color] == Red
</code></pre>

<p>One way is to use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html" rel="nofollow">.apply()</a>:</p>

<pre><code>In [83]: df
Out[83]: 
         Name   Color
0       Apple     NaN
1        Pear   Green
2        Pear   Green
3      Banana  Yellow
4  Watermelon   Green

In [84]: df['Color'] = df.apply(lambda x: 'Red' if x.Name == 'Apple' else x.Color, 1)

In [85]: df
Out[85]: 
         Name   Color
0       Apple     Red
1        Pear   Green
2        Pear   Green
3      Banana  Yellow
4  Watermelon   Green
</code></pre>

<p>Another simpler way is to use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.ix.html" rel="nofollow">.ix</a>:</p>

<pre><code>In [94]: df
Out[94]: 
         Name   Color
0       Apple     NaN
1        Pear   Green
2        Pear   Green
3      Banana  Yellow
4  Watermelon   Green

In [95]: df.ix[df.Name == 'Apple', 'Color'] = 'Red'

In [96]: df
Out[96]: 
         Name   Color
0       Apple     Red
1        Pear   Green
2        Pear   Green
3      Banana  Yellow
4  Watermelon   Green
</code></pre>

python pandas