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>Here's a way:</p>

<pre><code>df[df.columns.isin(['a', 'b', 'c']) &amp; (df &lt; 0)] = np.nan
</code></pre>

python pandas