Pandas data frames fill null values โ€‹โ€‹with index

I have a dataframe where for one column I want to fill in the null values โ€‹โ€‹with an index value. What is the best way to do this?

Say my dataframe looks like this:

>>> import numpy as np >>> import pandas as pd >>> d=pd.DataFrame(index=['A','B','C'], columns=['Num','Name'], data=[[1,'Andrew'], [2, np.nan], [3, 'Chris']]) >>> print d Num Name A 1 Andrew B 2 NaN C 3 Chris 

I can use the following line of code to get what I'm looking for:

 d['Name'][d['Name'].isnull()]=d.index 

However, I get the following warning: "The value is trying to be set on a copy of the slice from the DataFrame"

I suppose it would be better to do this using either fillna or loc, but I cannot figure out how to do this. I tried the following:

 >>> d['Name']=d['Name'].fillna(d.index) >>> d.loc[d['Name'].isnull()]=d.index 

Any suggestions that are the best option?

+4
source share
2 answers

IMO, you should use fillna , because the Index type is not a valid data type for the fill value that you want to pass to the series. Index has a to_series method:

 In [13]: d=pd.DataFrame(index=['A','B','C'], columns=['Num','Name'], data=[[1,'Andrew'], [2, np.nan], [3, 'Chris']]) d['Name']=d['Name'].fillna(d.index.to_series()) d Out[13]: Num Name A 1 Andrew B 2 B C 3 Chris 
+4
source

I would use .loc in this situation as follows:

 d.loc[d['Name'].isnull(), 'Name'] = d.loc[d['Name'].isnull()].index 
+2
source

All Articles