I have a DataFrame which contains nan values. I would like to fill those nans with the index value. The actual use case is filling the nans with a string template containing the index value which you can answer as a bonus.
Given:
In [31]: df
Out[31]:
0 1 2 3
0 NaN 0.069419 NaN NaN
1 2.439000 1.943944 0.279904 0.755746
2 0.013795 1.189474 0.834894 2.202108
3 0.520385 NaN NaN 1.451822
4 0.153863 0.957394 NaN 0.052726
5 1.274204 NaN NaN 0.169636
6 NaN 1.031703 NaN 0.267850
7 0.419157 NaN NaN 0.409045
8 NaN 1.526764 0.947936 0.442226
9 NaN NaN NaN 0.458331
and
In [35]: tmp
Out[35]: 'i=%(idx)s'
Output should be something like the following:
0 1 2 3
0 i=0 0.069419 i=0 i=0
1 2.439000 1.943944 0.279904 0.755746
2 0.013795 1.189474 0.834894 2.202108
3 0.520385 i=3 i=3 1.451822
4 0.153863 0.957394 i=4 0.052726
5 1.274204 i=5 i=5 0.169636
6 i=6 1.031703 i=6 0.267850
7 0.419157 i=7 i=7 0.409045
8 i=8 1.526764 0.947936 0.442226
9 i=9 i=9 i=9 0.458331
Just trying to fill the nans with the index.
Tried
In [32]: df.fillna(df.index)
ValueError: invalid fill value with a <class 'pandas.core.index.Int64Index'>
Tried
In [33]: df.replace(np.nan, df.index)
TypeError: Invalid "to_replace" type: 'float'
Tried
In [41]: df.fillna(df.index.values)
ValueError: invalid fill value with a <type 'numpy.ndarray'>
Tried
In [53]: df1 = df.astype(object)
and repeating the above, received same errors.
Using pandas==0.17.1