Replace 0 with blank in dataframe Python pandas
<p>I made the following code that takes out all of the zero's from my df. However when there is a number containing a zero it takes them out as well.</p>

<pre><code>e.g.
3016.2     316.2
   0.235      .235


data_usage_df['Data Volume (MB)'] = data_usage_df['Data Volume (MB)'].str.replace('0', '')
</code></pre>

<p>Could you help me to figure out how I do an exact match of the cell that equals 0 and replace it with a blank value.</p>

<p>I think you need add <code>^</code> for matching start of string and <code>$</code> for end of string:</p>

<pre><code>data_usage_df['Data Volume (MB)']=data_usage_df['Data Volume (MB)'].str.replace('^0.0$', '')
</code></pre>

<p>Sample:</p>

<pre><code>data_usage_df = pd.DataFrame({'Data Volume (MB)':[3016.2, 0.235, 1.4001, 0, 4.00]})

print (data_usage_df)
runfile('C:/Dropbox/work-joy/so/_t/test.py', wdir='C:/Dropbox/work-joy/so/_t')
   Data Volume (MB)
0         3016.2000
1            0.2350
2            1.4001
3            0.0000
4            4.0000

data_usage_df['Data Volume (MB)'] = data_usage_df['Data Volume (MB)'].astype(str)
data_usage_df['Data Volume (MB)']=data_usage_df['Data Volume (MB)'].str.replace('^0.0$', '')

print (data_usage_df)
  Data Volume (MB)
0           3016.2
1            0.235
2           1.4001
3                 
4              4.0
</code></pre>

<p>Another solution is converting column <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="nofollow"><code>to_numeric</code></a> and where is <code>0</code> give empty space:</p>

<pre><code>data_usage_df['Data Volume (MB)'] = data_usage_df['Data Volume (MB)'].astype(str)

data_usage_df.ix[pd.to_numeric(data_usage_df['Data Volume (MB)'], errors='coerce') == 0, 
                                                              ['Data Volume (MB)']] = ''

print (data_usage_df)
  Data Volume (MB)
0           3016.2
1            0.235
2           1.4001
3                 
4              4.0
</code></pre>

python pandas replace