-1

enter image description here

How do I replace the NaN values to 0 while keeping it in this index form?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Beaty
  • 21
  • 5

1 Answers1

0

You can process the original index as a Series first and then re-assign the index:

import pandas as pd
import numpy as np
df = pd.DataFrame({'Column1': [1, 2, 3, 4], 'Column2': [5, 6, 7, 8], 
'Column3': [8, 9, 10, 11]}, index=['a', 'b', np.nan, np.nan])
df.index = pd.Series(df.index).replace(np.nan, 0)
print df

Output:

          Column1  Column2  Column3
a           1         5        8
b           2         6        9
0           3         7        10
0           4         8        11
Anonymous
  • 659
  • 6
  • 16