Search code examples
pythonpandasdataframedictionaryfillna

replace pandas dataframe None value with dictionary


I have a pandas dataframe called "myRawDF1" and:

print(myRawDF1)

Result is:

    stop_price                                 last_trail_price
0        79.74     {'amount': '100.47', 'currency_code': 'USD'}
1                                                          None
2        73.06     {'amount': '114.52', 'currency_code': 'USD'}
etc...

I want to replace "None" with a dictionary {'amount': '0.0'} So the result would be:

    stop_price                                 last_trail_price
0        79.74     {'amount': '100.47', 'currency_code': 'USD'}
1                                             {'amount': '0.0'}
2        73.06     {'amount': '114.52', 'currency_code': 'USD'}

Or if it is somehow easier... I could use {'amount': '0.0', 'currency_code': 'USD'} like this:

    stop_price                                 last_trail_price
0        79.74     {'amount': '100.47', 'currency_code': 'USD'}
1                     {'amount': '0.0', 'currency_code': 'USD'}
2        73.06     {'amount': '114.52', 'currency_code': 'USD'}

I can't figure out how to do this. I thought I could use "fillna" because:

myRawDF1.fillna(0.0, inplace=True)

Successfully replaces all None values with zeros... So I thought this would work:

myRawDF1.fillna({'amount': '0.0'}, inplace=True)

But it doesn't... I also tried:

myRawDF1['last_trail_price'].fillna({'amount': '0.0'}, inplace=True)

This changes all the None vales to NAN... so its doing something

I also tried this... Which I found online... But it doesn't seem to work either

myRawDF1['last_trail_price'] = myRawDF1['last_trail_price'].fillna(pd.Series([{'amount': '0.0'}], index = myRawDF1.index))

Solution

  • I think you need to use loc access:

    s = df['last_trail_price'].isna()
    
    df.loc[s, 'last_trail_price'] = [{'amount':0.0} for _ in range(s.sum())]