Home > database >  How do I create a nested loop for multiple pandas dataframs
How do I create a nested loop for multiple pandas dataframs

Time:04-14

I'm pretty new to all of this and this is what I have:

import pandas as pd

Loop_List = ['ONE','TWO']

df = pd.DataFrame(table)
df2 = pd.DataFrame(table)

frames = [df,df2]
names = ["df","df2"]

for Dataset in Loop_List:
    print(Dataset)
    for name, data in zip(names, frames):
        print(f'  {name}')

Output:

ONE
  df
  df2
TWO
  df
  df2

The output that I'm looking to get is like this:

ONE
  df
TWO
  df2

CodePudding user response:

Please try this: please change the loop to the following:

for i,Dataset in enumerate(Loop_List):
    print(Dataset)
    print(f'  {names[i]}')

The entire code:

import pandas as pd

Loop_List = ['ONE','TWO']
df = pd.DataFrame(table)
df2 = pd.DataFrame(table)

frames = [df,df2]
names = ["df","df2"]

for i,Dataset in enumerate(Loop_List):
    print(Dataset)
    print(f'  {names[i]}')

CodePudding user response:

import pandas as pd
import numpy as np
df = pd.DataFrame( data=np.ones([4,4]) )
df2 = pd.DataFrame( data=np.ones([4,4]) )
df.name = 'First'
df2.name = 'Second'
df_list = [df,df2]
for i in df_list:
    print(i.name)
    print(i)
  • Related