Pandas: How to conditionally assign multiple columns?
<p>I want to replace negative values with <code>nan</code> for only certain columns. The simplest way could be:</p>

<pre><code>for col in ['a', 'b', 'c']:
    df.loc[df[col ] &lt; 0, col] = np.nan
</code></pre>

<p><code>df</code> could have many columns and I only want to do this to specific columns. </p>

<p>Is there a way to do this in one line? Seems like this should be easy but I have not been able to figure out.</p>

<p>use <code>loc</code> and <code>where</code></p>

<pre><code>cols = ['a', 'b', 'c']
df.loc[:, cols] = df[cols].where(df[cols].whwere.ge(0), np.nan)
</code></pre>

<p><strong><em>demonstration</em></strong></p>

<pre><code>df = pd.DataFrame(np.random.randn(10, 5), columns=list('abcde'))
df
</code></pre>

<p><a href="https://i.stack.imgur.com/LT639.png" rel="nofollow"><img src="https://i.stack.imgur.com/LT639.png" alt="enter image description here"></a></p>

<pre><code>cols = list('abc')
df.loc[:, cols] = df[cols].where(df[cols].ge(0), np.nan)
df
</code></pre>

<p><a href="https://i.stack.imgur.com/62dsI.png" rel="nofollow"><img src="https://i.stack.imgur.com/62dsI.png" alt="enter image description here"></a></p>

<hr>

<p>You could speed it up with numpy</p>

<pre><code>df[cols] = np.where(df[cols] &lt; 0, np.nan, df[cols])
</code></pre>

<p>to do the same thing.</p>

<hr>

<p><strong><em>timing</em></strong>  </p>

<pre><code>def gen_df(n):
    return pd.DataFrame(np.random.randn(n, 5), columns=list('abcde'))
</code></pre>

<p>since assignment is an important part of this, I create the <code>df</code> from scratch each loop.  I also added the timing for <code>df</code> creation.</p>

<p>for <code>n = 10000</code></p>

<p><a href="https://i.stack.imgur.com/3jaVi.png" rel="nofollow"><img src="https://i.stack.imgur.com/3jaVi.png" alt="enter image description here"></a></p>

<p>for <code>n = 100000</code></p>

<p><a href="https://i.stack.imgur.com/nGnNQ.png" rel="nofollow"><img src="https://i.stack.imgur.com/nGnNQ.png" alt="enter image description here"></a></p>

python pandas