doctorer doctorer - 12 days ago 6
Python Question

Conditional replacement of objects in a pandas dataframe

I have 2 dataframes

df_1
and
df_2
. Both have an index
datetimecode
which is a
pd.datetime64
object, and a
temp
column. I want to iterate through
df_1
and replace all the NaN temperature values with the corresponding temp from
'df_2'
.

Something like this:

for index, row in df_1.iterows():
row['temp'] = df_2[index]['temp'] if row['temp'] ==np.nan


but this is invalid sytax

Answer

IIUC

df_1.fillna(df_2, inplace=True)

or

df_1.loc[df_1.temp.isnull(), 'temp'] = df_2.temp

demonstration

tidx = pd.date_range('2016-03-31', periods=5)
df_1 = pd.DataFrame(dict(temp=[1, np.nan, 3, np.nan, 5]), tidx)
df_2 = pd.DataFrame(dict(temp=np.arange(11, 16)), tidx)

df_1.fillna(df_2)

enter image description here

df_1.loc[df_1.temp.isnull(), 'temp'] = df_2.temp

df_1

enter image description here

Comments