1

I have the following variable

my_res = RecognizeEntitiesResult(id=0, entities=[
    CategorizedEntity(text=50,000, category=Quantity, subcategory=Number, offset=10, length=6, confidence_score=0.8), 
    CategorizedEntity(text=infectious, category=Skill, subcategory=None, offset=29, length=10, confidence_score=0.8)], 
    warnings=[], statistics=None, is_error=False)

I want to have a Dataframe where each CategorizedEntity is a row, and the column for each row is the text, the category, and the confidence_score.

I tried the below, but with no success.

[x.as_dict() for x in my_res]

I could of course manually go through each CategorizedEntity and create a new row one by one but that doesnt seem very Pandas/Python-ic.

Any better way of doing this?

1 Answer 1

1

I'm afraid the most simple way to map data to dictionary would be:

from azure.ai.textanalytics import RecognizeEntitiesResult
from azure.ai.textanalytics import CategorizedEntity
from collections import defaultdict
import pandas as pd

d = defaultdict(list)

my_res = RecognizeEntitiesResult(id=0, entities=[
    CategorizedEntity(text='50,000', category='Quantity', subcategory='Number', offset=10, length=6, confidence_score=0.8), 
    CategorizedEntity(text='infectious', category='Skill', subcategory=None, offset=29, length=10, confidence_score=0.8)], 
    warnings=[], statistics=None, is_error=False)

for e in my_res.entities:
  for k, v in e.items():
    if (k in ['text', 'category', 'confidence_score']):
      d[k].append(v)

df = pd.DataFrame(d)

df.head()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.