How do I replace the NaN values to 0 while keeping it in this index form?
Asked
Active
Viewed 378 times
1 Answers
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
-
This does not fill na in the index. Please update your code. – Joe Patten Jan 23 '19 at 05:24
-
@JoePatten I have updated the code. – Anonymous Jan 24 '19 at 06:42