Home > Software design >  How to split big pandas df into multiple ones using for loop?
How to split big pandas df into multiple ones using for loop?

Time:11-30

I have a following problem. I have a big dataframe data. I would like to split it into n small dataframes with a suffix _n. I tried:

for suffix in range(0, 20):
    rows = len(data) // 20
    data   f"_{suffix}" = data[suffix*rows : (suffix 1)*rows]

But I got an error Cannot assign to operator. How can I fix it please?

CodePudding user response:

You just need to take df slice in range [i,i n), where n is the number of rows you want in the smaller dataframe, and i is the start index for each of the small dataframes, then you can create a dictionary using dictionary comprehension.

n=5   # Max number of rows per dataframe
res={f'df_{i/n:.0f}':df.iloc[i:i   n, :] for i in range(0, df.shape[0], n)}

PS: replace df with the variable name for your dataframe.

  • Related