Home > Software design >  Problem with create dataframe dynamic for i = n
Problem with create dataframe dynamic for i = n

Time:06-02

I am trying to create a dynamic pandas dataframe based on the number of records read, where each record would be a column.

My logic has been to apply a cycle where "for i=1 in N", where N is a read data (string format) to create the columns. This is not quite right for me, I have tried some alternatives but without good results. I only get the last record of the read.

I leave a proposal:

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 entity in doc.entities:
            for i in doc.entities:
                d = {'col'   i : [format(entity.text)]}
                df = pd.DataFrame(data=d)
                df_final = pd.concat([df_final, df], axis=1)
                display(df_final)
                i = i   1

funct_example(client)

What alternative do you recommend?

SOLUTION:

    for idx, doc in enumerate(docs):
    for entity in doc.entities:
        name = 'col'   str(i)
        d = {name : [format(entity.text)]}           
        df = pd.DataFrame(data=d)
        df_final = pd.concat([df_final, df], axis=1)
        i = i   1
        display(df_final)

Thanks you!

CodePudding user response:

this is because df is getting reassigned after each iteration.

here is one way to accomplish it

declare an empty DF before the start of the for loop

df_final = pd.DataFrame()

add after you have created the df df = pd.DataFrame(data=d)

df_final = pd.concat([df_final, df], axis=1)

this appends to your df_final

  • Related