Updating Pandas dataframe with "new information" when it includes NaNs

Viewed 252

Updating Pandas dataframe with "new information"

What I'd like to do is build a function that does the following (pseudo code):

def update(original_information, new_information):
    ... stuff ...
    return updated_information

Both the inputs and the output above are Pandas dataframes, all of which can contain a number of NaNs.

an example of original_information:

import pandas as pd
columns = ['edgar', 'morningstar', 'yahoo']
companies = [{'edgar': '0000320193', 'yahoo': 'AAPL'}, {'morningstar': 'XYZ', 'yahoo': 'SGO.PA'}]
original_information = pd.DataFrame(companies, columns=columns).sort_values('yahoo').reset_index(drop=True)

   edgar        morningstar  yahoo
0  00000320193  NaN          AAPL
1  NaN          XYZ          SGO.PA

an example of new information:

import pandas as pd
columns = ['edgar', 'morningstar', 'yahoo']
companies = [{'morningstar': 'AAPL', 'yahoo': 'AAPL'}, {'morningstar': 'XPAR:SGO', 'yahoo': 'SGO.PA'}]
new_information = pd.DataFrame(companies, columns=columns).sort_values('yahoo').reset_index(drop=True)

   edgar  morningstar  yahoo
0  NaN    AAPL         AAPL
1  NaN    XPAR:SGO     SGO.PA

Ideally my update function would accomplish three things:

  1. In cell (1, morningstar), update 'XYZ' with 'XPAR:SGO' since 'XPAR:SGO' is newer information
  2. In cell (0, morningstar), show 'AAPL' since the original information was 'NaN'
  3. In cell (0, edgar), keep '000320192' because there is no new information for that cell

In other words, what I'd like updated_information to look like is the following:

   edgar        morningstar  yahoo
0  00000320193  AAPL         AAPL
1  NaN          XPAR:SGO     SGO.PA

However, I haven't been able to figure out how to do this yet

What I have is the following:

def update(original_information, new_information):
    result = pd.concat([original_information, new_information], ignore_index=True)
    result = result.drop_duplicates(subset='yahoo', keep='last').sort_values('yahoo').reset_index(drop=True)
    return result

which returns:

   edgar  morningstar  yahoo
0  NaN    AAPL         AAPL
1  NaN    XPAR:SGO     SGO.PA

which...

  1. successfully replaces 'XYZ' with 'XPAR:SGO' in cell (1, morningstar)

  2. successfully updates cell (0, morningstar) with 'AAPL'

  3. but unfortunately deletes '000320192' in cell (0, edgar) instead of keeping it

Any suggestions?

1 Answers
Related