0

We have a list of 2 dataframes & want to create separate dataframes. Currently using the following code to achieve that:

for i in range(len(listOfDf)):
    globals()[f"df_{i}"] = listOfDf[i]

However, I want to name the dataframes as "Events" & "Occurance" respectively. How can this be achieved ?

Lopa
  • 51
  • 6
  • Get out of the habit of using `for index in range(len(list)):`. Use `for item in list:` or `for index, item in enumerate(list):` – Barmar Mar 21 '23 at 15:27
  • `globals()` also smells like a thing to avoid here, you probably don't want to be using it. – emmagras Mar 21 '23 at 15:30

2 Answers2

2

Use ordinary list unpacking:

events, occurrences = listOfDf

There's nothing special about doing this with dataframes.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I came up with the following code:

dataframes=["events", "occurrences" ]

# Unpack the list to create multiple dataframes
for i,j in zip(range(len(listOfDf)),dataframes):
    globals()[f"{j}"] = listOfDf[i]
    print("Columns of "+f"{j}"+" are :")
    print((globals()[f"{j}"]).columns)
Lopa
  • 51
  • 6