I have a database of historical stock trading. The frame has columns such as ['ticker', 'date', 'cusip', 'profit', 'security_type']. Initially:
trades['cusip'] = np.nan trades['security_type'] = np.nan
I have historical configuration files that I can load into frames with columns such as ['ticker', 'cusip', 'date', 'name', 'security_type', 'primary_exchange'].
I would like to UPDATE the frame frame using cusip and security_type from config, but only where the ticker and date match.
I thought I could do something like:
pd.merge(trades, config, on=['ticker', 'date'], how='left')
But this does not update the columns, it just adds configuration columns to transactions.
The following works, but I think there should be a better way. If not, I will probably do it outside of pandas.
for date in trades['date'].unique(): config = get_config_file_as_df(date) ## config['date'] == date for ticker in trades['ticker'][trades['date'] == date]: trades['cusip'][ (trades['ticker'] == ticker) & (trades['date'] == date) ] \ = config['cusip'][config['ticker'] == ticker].values[0] trades['security_type'][ (trades['ticker'] == ticker) & (trades['date'] == date) ] \ = config['security_type'][config['ticker'] == ticker].values[0]
source share