1

My dataframe look like this:

      Dividends        Volume  Close       Company    Sector
Year                                                        
2009   0.280000                10.35           ABC   Finance
2010   0.280000  5.264694e+06   9.88           ABC   Finance
2011   0.560000  5.153132e+06   8.26           ABC   Finance
2012   0.560000  3.930295e+06                  ABC   Finance
2013             4.085875e+06  13.16           ABC   Finance

I just like to replace the blank space with "0".

      Dividends        Volume  Close       Company    Sector
Year                                                        
2009   0.280000             0  10.35           ABC   Finance
2010   0.280000  5.264694e+06   9.88           ABC   Finance
2011   0.560000  5.153132e+06   8.26           ABC   Finance
2012   0.560000  3.930295e+06      0           ABC   Finance
2013          0  4.085875e+06  13.16           ABC   Finance

Any help would be very much appreciated. Many Thanks!

Lawrence How
  • 41
  • 1
  • 8
  • Does this answer your question? [Replacing blank values (white space) with NaN in pandas](https://stackoverflow.com/questions/13445241/replacing-blank-values-white-space-with-nan-in-pandas) – adnanmuttaleb Aug 09 '20 at 05:55

4 Answers4

1

Replace empty strings with 0.

cols = df.columns
df.loc[:, cols] = df.loc[:, cols].replace (r'\s*', 0, regex=True)
prashant0598
  • 203
  • 3
  • 9
1

I believe this is what you're looking for:

df = df.replace("", 0)
Active_Learner
  • 173
  • 1
  • 5
0

The question is whether the empty spaces are NaNs or just empty spaces.

If they are NaNs, then this code should be useful:

df.loc[:, 'Dividends':'Close'] = df.loc[:, 'Dividends':'Close'].fillna(0)
Luker
  • 141
  • 2
  • 4
0

You just have to search this before posting the question here. Learn to search and save your time. btw:

df = df.fillna(0)