How do you create multiple data frames using repeat statements?

Asked 2 years ago, Updated 2 years ago, 41 views

I want to make 10 data frames each using the for statement in Pandas.

However, when you actually run it, the repetition door turns 10 times and only one data frame, the last result, is created.

How do you create code to create 10 data frames with each content when you execute a for statement?

Between for and pd.DataFrame, we created a for statement that makes a list of names, years, and points

This part works well, but I can't make multiple data frames.

It's hard because I'm a beginner in coding.

for i in range(10):
    df = pd.DataFrame({'name' : list_name,
                       'year' : list_year,
                       'point' : list_point})

python pandas

2022-09-20 22:11

1 Answers

dfs = []
for i in range(10):
    df = pd.DataFrame( ... )
    # ...
    dfs.append(df)

dfs[0]
dfs[1]
...

You create an empty list, and you put one data frame from for into the list.

Outside the for statement, access the index (0-base) of which data frame it is.


2022-09-20 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.