Create multiple Dataframes through iteration

Asked

Viewed 69 times

0

Is it possible to create multiple Dataframes within an iteration? I need to do this feat, follow the example:

in the NUM iteration, it has numbers from 0 to 9, total 10 numbers. in the for nn in NUM: it will iterate renaming the df_nn getting df_0 df_1... until df_9, thus generating 10 different Dataframes, where I can query each one.

Is there any way to do this? Thank you

import pandas as pd
import random

NUM = [n for n in range(10)]

for nn in NUM:
    df_nn = pd.DataFrame({
        "A":[random.random(),random.random()]
    })

1 answer

2


It seems to me that you are looking for a dictionary, for example:

d = {
    n: pd.DataFrame({
        "A": [random.random(), random.random()]
    }) 
    for n in range(10)
}

It is now possible to access each Dataframe through its corresponding key (in this example, numbers from 0 to 9):

>>> d[0]
          A
0  0.592403
1  0.328729

>>> d[5]
          A
0  0.273226
1  0.188603
  • perfect, thank you for your availability, hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.