title: Filling Null Spots in CSV in Python
body: <p>I am parsing a csv file to create charts. I am able to do this with no problem, EXCEPT in a single case... Whenever there is a null slot in the csv file. For example:</p>
  
  <blockquote>
    <p>Col1 Col2 Col3 Col4 Col5<br>
    &nbsp; 45&nbsp; &nbsp;34  &nbsp; &nbsp; 23 &nbsp; &nbsp; 98 &nbsp; &nbsp; 18<br>
    &nbsp; 66&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 25 &nbsp; &nbsp; 0<br>
    &nbsp; 18&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 52 &nbsp; &nbsp; 56&nbsp; &nbsp; 100</p>
  </blockquote>
  
  <p>There are two blank entries in the file in column 2 and 5. I want to fill these spots with 0. I'm fairly new to Python. In the case where there is a null spot in the csv, I would like to insert a 0. Because I may sometimes have blanks in my csv file, I get the error <code>TypeError: unsupported operand type(s) for -: 'int' and 'str'</code>. It can be tiresome to have to go into the csv file to check whether there is a null spot and manually fill it with zero so I would like a way to do this in the script. Here is my code:</p>
  
  <pre><code>import pandas as pd
  import matplotlib.pyplot as plt
  import numpy as np
  
  
  file_name = "myfile.csv"
  df = pd.read_csv(file_name)
  names = df['name'].values
  
  x = np.arange(len(names))*2
  w = 0.40
  
  col2 = df.columns[1]
  col3 = df.columns[2]
  col4 = df.columns[3]
  col5 = df.columns[4]
  
  dif = df[col4] - df[col3]
  
  colors = ['Red' if d &lt; -5 else 'Blue' for d in dif]
  
  plt.bar(x-w, df[col2].values, width=w*0.7, label=col2, color = "cyan")
  plt.bar(x, df[col3].values, width=w*0.7, label=col3, color = "green")
  plt.bar(x+w, df[col4].values, width=w*0.7, label=col4, color = colors)
  plt.plot(x, df[col5].values, lw=2, label="Goal", color = "red")
  
  plt.xticks(x, names, rotation='vertical')
  plt.ylim([0,100])
  
  plt.show()
  </code></pre>
  
  <p>Note: As I mentioned above, I'm reading the dataframe from a csv file. </p>
  
  <p>EDIT:</p>
  
  <p>I have added this line to my code:</p>
  
  <pre><code>df.replace(r'^\s*$', 0, regex=True)
  #For testing purposes, I also added this:
  print(df.replace(r'^\s*$', 0, regex=True))
  </code></pre>
  
  <p>I can see that the empty slots are now filled with zeros but I am still getting the error <code>TypeError: unsupported operand type(s) for -: 'str' and 'int'</code> for <code>dif = df[col4] - df[col3]</code>. Is it possibly reading those inserted 0 as strings?
  I have also tried to wrap <code>df[col3]</code> and <code>df[col4]</code> in <code>int()</code> but no luck there. It gives the error <code>TypeError: cannot convert the series to &lt;class 'int'&gt;</code>. I then tried <code>df[col4].astype(int) - df[col3].astype(int)</code> and got the error <code>ValueError: invalid literal for int() with base 10</code>.</p>
  
  <p>EDIT 2: 
  I just added the line <code>print(df.dtypes)</code>. For some reason the fourth column (which was containing the replaced 0 in this case) is being seen as an object instead of int64 like the rest of the columns.</p>