0

I have a list of CUSIPs that I would like gather data for using pdblp (https://matthewgilbert.github.io/pdblp/tutorial.html).

For example:

List of CUSIPs (around 5,000 in total):

/cusip/xxxxxxxxx, /cusip/yyyyyyyyy ...

con = pdblp.BCon(debug=False, port=8194, timeout=5000)
con.start()

con.ref(['/cusip/xxxxxxxxx', '/cusip/yyyyyyyyy'], ['Feature1', 'Feature2', 'Feature3',...])

this gives me a dataframe:

Original DataFrame

However, I would like to create a data frame which has the features as columns with the CUSIPs as either another column or an index:

Wanted DataFrame

I wasn't sure if the bdh function would get me what I was looking for either given that it is for historical data (I am looking for most recent). I guess I could also just create a long DataFrame and just melt or pivot it?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
n8erg8er
  • 25
  • 3
  • Please include any relevant information [as text directly into your question](https://stackoverflow.com/editing-help), do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) – Henry Ecker Oct 26 '21 at 22:38
  • If you need assistance formatting a small sample of your DataFrame as a copyable piece of code for SO see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker Oct 26 '21 at 22:38

1 Answers1

2

You can create a pivot table:

df = con.ref(['/cusip/xxxxxxxxx', '/cusip/yyyyyyyyy'], ['Feature1', 'Feature2', 'Feature3',...])
df.pivot(index='ticker', columns='field', values='value')

Result:

ticker Feature1 Feature2 Feature3
/cusip/xxxxxxxxx value1 value2 value3
/cusip/yyyyyyyyy value1 value2 value3
RJ Adriaansen
  • 9,131
  • 2
  • 12
  • 26