Robsmith
Robsmith

Reputation: 473

Building Dataframe from For Loop using Pandas

I have a for-loop that returns the data below and I am trying to put it into a single data frame using Pandas. The issue is that each time I try I get separate data frames for each row instead of one single data frame. I'm sure the solution is simple but I just can't work it out.

For-Loop:

    for runner in i.runners:
        print(i.market_id, runner.selection_id, runner.runner_name)

Output:

1.193218924 36476468 Immortal Fame
1.193218924 27794475 After The Fox
1.193218924 21214404 Banana Joe
1.193218924 4177156 Bird On The Wire
1.193218924 24462868 Vinnies Getaway
1.193218924 28007020 Veshenskaya
1.193218924 10159485 Green Or Black
1.193218924 21001423 Definite Wisdom
1.193218924 26997545 Ribeye
1.193218924 21822171 Dorado Dollar
1.193218924 4678460 My Keepsake
1.193218924 8481071 Ballyfarsoon
1.193219097 38432851 Dynamite Kentucky
1.193219097 37413708 Voyburg
1.193219097 39371721 Dans Chosen
1.193219097 26548202 Terrierman
1.193219097 15684272 Daydream Aulmes
1.193219097 18570232 Johnny B
1.193219097 16519452 Chloes Court
1.193219097 28093620 Itacare

Attempt 1:

    for runner in i.runners:
        print(i.market_id, runner.selection_id, runner.runner_name)
        df = pd.DataFrame({
            'Market ID': i.market_id,
            'Selection ID': runner.selection_id,
            'Selection Name': runner.runner_name,
        }, index=[0])
        print(df)

Attempt 2:

    for runner in i.runners:
        d.append(
            {
                'Market ID': i.market_id,
                'Selection ID': runner.selection_id,
                'Selection Name': runner.runner_name
            }
        )

    pd.DataFrame(d)
    print(d)

Desired Output:

enter image description here

With for Loop to get Match IDs:

    for i in results:
         race_ids.append(i.market_id)

         for runner in i.runners:
             print(i.market_id, runner.selection_id, runner.runner_name)
             df = pd.DataFrame({
                 'Market ID': i.market_id,
                 'Selection ID': runner.selection_id,
                 'Selection Name': runner.runner_name,
             }, index=[0])
             print(df)

Upvotes: 1

Views: 67

Answers (3)

jezrael
jezrael

Reputation: 863741

Create list of dicts anmd pass to DataFrame constructor:

L = [ {'Market ID': i.market_id, 
       'Selection ID': runner.selection_id, 
       'Selection Name': runner.runner_name } for runner in i.runners]

Edited answer:

L = [ {'Market ID': i.market_id, 
       'Selection ID': runner.selection_id, 
       'Selection Name': runner.runner_name }  for i in results for runner in i.runners]

df = pd.DataFrame(L)

Upvotes: 1

user7864386
user7864386

Reputation:

Use list comprehension to collect all your data and cast it to a DataFrame:

data = [[i.market_id, runner.selection_id, runner.runner_name] for i in results for runner in i.runners]
df = pd.DataFrame(data, columns = ['Market ID', 'Selection ID', 'Selection Name'])

Upvotes: 2

Bemwa Malak
Bemwa Malak

Reputation: 1349

You can do it using a 2D list then converting this 2D list into a single data frame like this:

main_lst = list()
for runner in i.runners:
        lst = list()
        lst.append(i.market_id)
        lst.append(runner.selection_id)
        lst.append(runner.runner_name)
        main_lst.append(lst)
df = pd.DataFrame(main_lst, columns =['Market ID', 'Selection ID', 'Selection Name']) 
print(df)

Upvotes: 1

Related Questions