How to create separate columns for each RatingType found in RatingScope Column using python?

import refinitiv.data as rd
rd.open_session()
rd.discovery.search(
view=rd.discovery.Views.MUNICIPAL_INSTRUMENTS,
top=10,
filter="(IsActive eq true and SearchAllCategoryv2 eq 'US Municipal Bonds' and (((((((((((((((((((((((RatingsScope((RatingType eq 'FTC'))) or (RatingsScope((RatingType eq 'FUN')))) or (RatingsScope((RatingType eq 'FST')))) or (RatingsScope((RatingType eq 'FUS')))) or (RatingsScope((RatingType eq 'FDR')))) or (RatingsScope((RatingType eq 'FRR')))) or (RatingsScope((RatingType eq 'MDY')))) or (RatingsScope((RatingType eq 'MUN')))) or (RatingsScope((RatingType eq 'MLI')))) or (RatingsScope((RatingType eq 'MLH')))) or (RatingsScope((RatingType eq 'MST')))) or (RatingsScope((RatingType eq 'MSH')))) or (RatingsScope((RatingType eq 'MUS')))) or (RatingsScope((RatingType eq 'MSI')))) or (RatingsScope((RatingType eq 'MLN')))) or (RatingsScope((RatingType eq 'FDL')))) or (RatingsScope((RatingType eq 'FDS')))) or (RatingsScope((RatingType eq 'MDT')))) or (RatingsScope((RatingType eq 'MDR')))) or (RatingsScope((RatingType eq 'MIS')))) or (RatingsScope((RatingType eq 'MSS')))))))",
select="LongName,Ticker,CouponRate,MaturityDate,CUSIP,RIC,MuniStateDescription,CouponClassDescription,EJVAssetID,BusinessEntity,PI,SearchAllCategoryv3,SearchAllCategoryv2,SearchAllCategory,IssueDate,BankQualified,BondInsuranceDescription,IsCallable,IsPutable,IsSinkable,RatingsScope(filter:((RatingType xeq 'FTC') or (RatingType xeq 'FUN') or (RatingType xeq 'FST') or (RatingType xeq 'FUS') or (RatingType xeq 'FDR') or (RatingType xeq 'FRR') or (RatingType xeq 'MDY') or (RatingType xeq 'MUN') or (RatingType xeq 'MLI') or (RatingType xeq 'MLH') or (RatingType xeq 'MST') or (RatingType xeq 'MUS') or (RatingType xeq 'MSH') or (RatingType xeq 'MSI') or (RatingType xeq 'MLN') or (RatingType xeq 'FDL') or (RatingType xeq 'FDS') or (RatingType xeq 'MDT') or (RatingType xeq 'MDR') or (RatingType xeq 'MIS') or (RatingType xeq 'MSS'))),RatingX1XRatingRank"
)
When I run this code in Codebook, the result is a single field, RatingScope Column, containing multiple dictionaries grouped by RatingType.
For example, a cell in RatingScope contains the following data:
[{'Agency': 'MDY', 'CurrentRating': 'A1', 'CurrentRatingDate': '2023-10-03T00:00:00.000Z', 'CurrentRatingRank': 5, 'Preliminary': 'n', 'RatingScope': 'D', 'RatingType': 'MDT', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'Aa3', 'CurrentRatingDate': '2020-04-23T00:00:00.000Z', 'CurrentRatingRank': 4, 'Preliminary': 'n', 'RatingScope': 'D', 'RatingType': 'MDR', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'MIG 1', 'CurrentRatingDate': '2024-06-17T00:00:00.000Z', 'CurrentRatingRank': 1, 'Preliminary': 'n', 'PreviousRatingDate': '2023-10-03T00:00:00.000Z', 'PreviousRatingRank': 1, 'RatingType': 'MUS', 'Solicited': 'y'},
{'Agency': 'MDY', 'CurrentRating': 'MIG 1', 'CurrentRatingDate': '2024-06-17T00:00:00.000Z', 'CurrentRatingRank': 1, 'Preliminary': 'n', 'PreviousRatingDate': '2023-10-03T00:00:00.000Z', 'PreviousRatingRank': 1, 'RatingType': 'MST', 'Solicited': 'y'}]
My question is: instead of pulling the entire RatingScope with multiple dictionaries per RatingType, how can I modify the code to retrieve separate columns for each RatingType? Specifically, I would like to extract the Agency, CurrentRating, and CurrentRatingDate for each of the 21 rating types, resulting in 63 columns (3 per RatingType).
For example:
- Column 1: 'RatingType': 'MDT', 'Agency'
- Column 2: 'RatingType': 'MDT', 'CurrentRating'
- Column 3: 'RatingType': 'MDT', 'CurrentRatingDate'
- Column 4: 'RatingType': 'MDR', 'Agency'
- Column 5: 'RatingType': 'MDR', 'CurrentRating'
- Column 6: 'RatingType': 'MDR', 'CurrentRatingDate'
And so on for all 21 rating types.
I appreciate your assistance and look forward to your suggestions.
Best Answer
-
Hi @dianne.palmario ,
Although as much as I know it is not natively possible with the library, you can leverage python capabilities to achieve it. Below I have implemented a small function that does that based on my understanding:
import pandas as pd
def extract_rating_type_columns(df):
for index, row in df.iterrows():
for rating_dict in row['RatingsScope']:
rating_type = rating_dict['RatingType']
df.loc[index, f'{rating_type}_Agency'] = rating_dict.get('Agency')
df.loc[index, f'{rating_type}_CurrentRating'] = rating_dict.get('CurrentRating')
df.loc[index, f'{rating_type}_CurrentRatingDate'] = rating_dict.get('CurrentRatingDate')
extract_rating_type_columns(df)
df = df.drop(columns=['RatingsScope'])
dfIs this what you were looking for?
Best regards,
Haykaz
0
Answers
-
@aramyan.h - this is it! How do I incorporate it with the formula so that the other columns would also be there while these separated columns are there?Hello0
-
just assign your RD call to df variable and the call th function I have provided:
df = rd.discovery.searh(...)
0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛