Wouter Vandenputte
Wouter Vandenputte

Reputation: 2103

Python DataFrame: split to lists

I've a DataFrame df of let us say one thousand rows, and I'd like to split it to 10 lists where each list contains a DataFrame of 100 rows. So list

zero = df[0:99]
one  = df[100:199]
two  = df[200:299]
...
nine = df[900:900]

What could be a good (preferably) oneliner for this?

Upvotes: 0

Views: 43

Answers (2)

Mayank Porwal
Mayank Porwal

Reputation: 34056

Like this maybe:

list_of_dfs = [df.loc[i:i+size-1,:] for i in range(0, len(df),1000)]

Upvotes: 1

Christian Sloper
Christian Sloper

Reputation: 7510

Assuming index is a running integer (can use .reset_index() if not)

[d for g,d in df.groupby(df.index//100)]

Returns a list of dataframes.

Upvotes: 2

Related Questions