0

I am trying to create a pandas dataframe dynamically. So far I'm fine with capturing data within the dataframe, but not with the name of the columns.

I also want the name of the columns to be based on the 'category' of the data that comes with the record that I am reading in my function, but I always get the last one.

def funct_example(client):
documents = [ v_document ]

poller = client.begin_analyze_entities(documents)
result = poller.result()

docs = [doc for doc in result if not doc.is_error]
i = 1    
df_final = pd.DataFrame()       
    
for idx, doc in enumerate(docs):      
    for relation in doc.entity_relations:
        for role in relation.roles:
            name = str([format(entity.category)]) + str(i)  # <---- THIS LINE ALWAYS IS THE LAST REGISTER

            d = {name : "'{}' with entity '{}'".format(role.name, role.entity.text)} # <---THIS IS OK                 
            df = pd.DataFrame(data=d, index=[0])                
            df_final = pd.concat([df_final, df], axis=1)
            i = i + 1
            display(df_final)
return(df_final)
df_new_2 = funct_example(client)

I've tried adding an extra loop between creating the dataframe sentence and concat function like so:

for col in df.columns:
    name = str([format(entity.category)]) + str(i)  
    df = df.rename(columns={col: name })  

But the last category still appears in the column name...

How can I solve that?

From already thank you very much.

SOLUTION:

    for idx, doc in enumerate(docs):      
    for relation in doc.entity_relations:
        for role in relation.roles:
            name = 'Relation_' + format(relation.relation_type) + '_' + str(i)  
            d = {name : "'{}' with entity '{}'".format(role.name, role.entity.text)} 
            df = pd.DataFrame(data=d, index=[0])
            df_final = pd.concat([df_final, df], axis=1)
            i = i + 1
            display(df_final)
return(df_final)
df_relations = funct_example(client)

Regards!! :D

3
  • Please provide an example of the desired output. Commented May 31, 2022 at 0:56
  • 1
    Maybe first use print() (and print(type(...)), print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called "print debuging" and it helps to see what code is really doing. Commented May 31, 2022 at 1:21
  • Thanks furas for you time. I gave me an idea with your comment. I postead a update in the code with solution. Thanks you!! Commented May 31, 2022 at 12:47

1 Answer 1

1

its difficult to suggest a solution without knowing the properties of all the objects being used.

'entity' object, is not defined within the function, so is it a global variable?

Does the role has 'entity', which then has the 'category' property? Im assuming as such, since role does have entity property

d = {name : "'{}' with entity '{}'".format(role.name, role.entity.text)} # <---THIS IS OK

Beside, the name variable while initialized is not used.

maybe you try

name = str([format(role.entity.category)]) + str(i)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you for you time Naveed. You gave me an idea with your comment, I have divided the analysis according to the desired output and I have managed to find the solution. I just put a RETURN() outside the loop along with the display(df). I have updated the code with the solution. Thank you!!

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.