Merging historical and forecast dataframe yields duplicates
I get duplicate cells when I merge two dataframes: one containing historical data, a second containing forecast data, as follows:
tkrrenew = ['0916.HK', '0958.HK', '0451.HK', '1798.HK']
fdflds = ['CF_NAME', 'TR.EBITDA.rfperiod', 'TR.BasicEpsExclExtraItems.Currency', 'TR.BasicEpsExclExtraItems', 'TR.DpsCommonStock']
param = {'Period': 'FY0', 'SDate': 'FY-2', 'EDate': 'FY0', 'FRQ': 'FY'}
valrenew, err = ek.get_data(tkrrenew, fdflds, param)
frcstfld = ['CF_NAME', 'TR.EPSSmartEst.rfperiod', 'TR.EPSSmartEst', 'TR.DPSMean']
paramfcst = {'Period': 'FY1', 'SDate': '0', 'EDate': '2', 'FRQ': 'FY'}
fcstrenew, err = ek.get_data(tkrrenew, frcstfld, paramfcst)
testrenew = valrenew.merge(fcstrenew, on=['Instrument', 'Name'], how='inner')
The output, after some other commands is as follows:
Apart from .drop_duplicate(subset=['Instrument', 'FY']) which leaves me with:
What can I do to have both historical and forecast data till FY3 in one dataframe. If .join or .append can place the FY1-FY3 in the same column as FY-2 to FY0, that would ideal.
Pls advise.
Best Answer
-
Is this what you're looking for?
valrenew.merge(fcstrenew, on=['Instrument', 'Financial Period Relative' ], how='outer')
This will give you
You can then sort the resulting dataframe on Instrument and then Financial Period Relative column to get chronological view grouped by the stock. You can remove duplicate CF_NAME column (CF_NAME_x or CF_NAME_y), or you can drop CF_NAME field from one of the requests.0
Answers
-
Thanks, Alex & Wasin.
I've rewritten my code such that it now goes as follows:
dffrkFY0 = dffrank[dffrank.FY == 'FY0'] #extract FY0 data
dffrkFY1 = dffrank[dffrank.FY == 'FY-1'] #extract FY-1 data
dffrkFY1.columns = [['Instrument', 'Name', 'FYE', 'RpCrncy','EstEPS','EstDPS','EstBVPS','PxCrncy','Price',\
'FXCode', 'FXRate', 'EstPER', 'EstPB', 'EstD_Yld']]
# dffwd is an earlier DataFrame
dffmerge = pd.concat([dffwd, dffrkFY0, dffrkFY1], join='inner')
dffmerge = dffmerge.sort_values(['Instrument', 'FYE'])
dffmerge = dffmerge.reset_index(drop=True)
dffmerge[:15]Thanks for your effort; much appreciated.
Later on in the code, I re-jig the <Name> such that it doesn't appear at such an odd position.
0
Categories
- All Categories
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛