question

howard.ko@uci.edu avatar image
0 Likes"
howard.ko@uci.edu asked ·

Getting screener results into Pandas Dataframe

Hi, I'm having trouble getting screen results into a pandas dataframe. I can successfully use the

api_response.property_name_dict

feature via the Python SDK for other results, such as for stock prices etc... But it looks like screener results come back as a list, and there is no '_dict' version of any of the screener properties. Any suggestions? Here's an example screen taken from another post.


security_api = intrinio_sdk.SecurityApi()
 
 
clause1 = intrinio_sdk.SecurityScreenClause(field ='employees', operator = 'gt', value = 100000)
clause2 =  intrinio_sdk.SecurityScreenClause(field = 'state', operator = 'eq', value = 'California')
 
logic = intrinio_sdk.SecurityScreenGroup(operator ="AND", clauses =[clause1, clause2])# SecurityScreenGroup | The logic to screen with, consisting of operators, clauses, and nested groups.<br/> See <a href=\"/documentation/screener_v2\" target=\"_blank\">screener documentation</a> for details on how to construct conditions. (optional)
order_column = 'employees' # str | Results returned sorted by this column (optional)
order_direction = 'desc' # str | Sort order to use with the order_column (optional) (default to asc)
primary_only = True # bool | Return only primary securities (optional) (default to False)
page_size = 100 # int | The number of results to return. Maximum for this endpoint is 50000. (optional) (default to 100)
 
try:
  api_response = security_api.screen_securities(logic=logic, order_column=order_column, order_direction=order_direction, primary_only=primary_only, page_size=page_size)
  pprint(api_response)
except ApiException as e:
  print("Exception when calling SecurityApi->screen_securities: %s\r\n" % e)


The api_response comes back as a list:

[{'data': [{'number_value': '132000.0', 'tag': 'employees', 'text_value': None},
          {'number_value': None, 'tag': 'state', 'text_value': 'California'}],
 'security': {'code': None,
              'company_id': 'com_NX6GzO',
              'composite_figi': None,
              'composite_ticker': 'AAPL:US',
              'currency': None,
              'figi': 'BBG000B9Y5X2',
              'id': 'sec_agjrgj',
              'name': 'Apple Inc',
              'share_class_figi': None,
              'ticker': 'AAPL'}},
 {'data': [{'number_value': '107400.0', 'tag': 'employees', 'text_value': None},
          {'number_value': None, 'tag': 'state', 'text_value': 'California'}],
 'security': {'code': None,
              'company_id': 'com_NgYGzd',
              'composite_figi': None,
              'composite_ticker': 'INTC:US',
              'currency': None,
              'figi': 'BBG000C0GFS4',
              'id': 'sec_zvNxEz',
              'name': 'Intel Corp.',
              'share_class_figi': None,
              'ticker': 'INTC'}}]

Running the following would return:

pd.DataFrame(api_response)

0
0 {'data': [{'number_value': '132000.0', 'tag': ...
1 {'data': [{'number_value': '107400.0', 'tag': ...


There are no other _dict objects available for me to use.


Thanks!

pythonscreener
· 1
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

This is an outstanding question, We hope to have an example ready soon.

0 Likes 0 · ·

1 Answer

· Write an Answer
mkelly avatar image
0 Likes"
mkelly answered ·

Hi! Our devs came up with the following which should help, Note that this will only work in Python 3.

.
.
.
logic = intrinio_sdk.SecurityScreenGroup(operator ="AND", clauses =[clause1, clause2])
order_column = 'employees'
order_direction = 'desc'
primary_only = True 
page_size = 100

try:
  api_response = security_api.screen_securities(
      logic=logic, 
      order_direction=order_direction, 
      primary_only=primary_only,  
      page_size=page_size,
  )
  
except ApiException as e:
  print("Exception when calling SecurityApi->screen_securities: %s\r\n" % e)

data = []

for x in api_response:

    record = {}

    for attr, value in x.security.__dict__.items():
        if attr.startswith('_'):
            record[attr.replace('_', '')] = value

    for d in x.data:    
        record[d.tag] = d.number_value or d.text_value
    data.append(record)
    
data_frame = pd.DataFrame(data)
print(data_frame)


· Share
10 |600 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.