Binarize a float64 Pandas Dataframe in Python
<p>I've got a Panda DF with various columns (each indicating the frequency of a word in a corpus). Each row corresponds to a document and each is of type float64. </p>

<p>for example:</p>

<pre><code>word1 word2 word3
0.0   0.3   1.0
0.1   0.0   0.5
etc
</code></pre>

<p>I want to Binarize this and instead of the frequency end up with a boolean (0s and 1s DF) that indicates the existence of a word</p>

<p>so the above example would be transformed to :</p>

<pre><code>word1 word2 word3
0      1     1
1      0     1
etc
</code></pre>

<p>I looked at get_dummies(), but the output was not the expected.</p>

<p>Code:</p>

<pre><code>import numpy as np
import pandas as pd

""" create some test-data """
random_data = np.random.random([3, 3])
random_data[0,0] = 0.0
random_data[1,2] = 0.0

df = pd.DataFrame(random_data,
     columns=['A', 'B', 'C'], index=['first', 'second', 'third'])

print(df)

""" binarize """
threshold = lambda x: x &gt; 0
df_ = df.apply(threshold).astype(int)

print(df_)
</code></pre>

<p>Output:</p>

<pre><code>A         B         C
first   0.000000  0.610263  0.301024
second  0.728070  0.229802  0.000000
third   0.243811  0.335131  0.863908
A  B  C
first   0  1  1
second  1  1  0
third   1  1  1
</code></pre>

<p>Remarks:</p>

<ul>
<li>get_dummies() analyze each unique value per column and introduces new columns (for each unique value) to mark if this value is active</li>
<li>= if column A has 20 unique values, 20 new columns are added, where exactly one column is true, the others are false</li>
</ul>

python pandas dataframe