Convert pandas python data frame column values in place
<p>I have a column of positive and negative numbers and I want to convert it to a list of 0s and 1s. If the number if positive, it should be replaced with a 1. If the number is negative or 0, it should be replaced by a 0. How can I do this?</p>

<p>For example, in R, I would do:</p>

<pre><code>list = ifelse(list &gt; 0, 1, 0)
</code></pre>

<p>You can use <code>DataFrame.apply</code> to apply a function to each row of your DataFrame. For instance:</p>

<pre><code>import numpy as np
import pandas as pd
df = pd.DataFrame({'A': [1, -2, 3, -4]})
df['A'] = df.apply(lambda row:np.where(row['A'] &gt; 0, 1, 0), axis=1)
</code></pre>

<p>The <code>lambda</code> function can be replaced with any function (doesn't have to be a <code>lambda</code>), and <code>axis=1</code> is to apply the function to each row rather than each column.</p>

<p><a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.apply.html</a></p>

python pandas dataframe