Be aware that np.array_split(df, 3)
splits the dataframe into 3 sub-dataframes, while the split_dataframe
function defined in @elixir's answer, when called as split_dataframe(df, chunk_size=3)
, splits the dataframe every chunk_size
rows.
Example:
With np.array_split
:
df = pd.DataFrame([1,2,3,4,5,6,7,8,9,10,11], columns=['TEST'])
df_split = np.array_split(df, 3)
...you get 3 sub-dataframes:
df_split[0] # 1, 2, 3, 4
df_split[1] # 5, 6, 7, 8
df_split[2] # 9, 10, 11
With split_dataframe
:
df_split2 = split_dataframe(df, chunk_size=3)
...you get 4 sub-dataframes:
df_split2[0] # 1, 2, 3
df_split2[1] # 4, 5, 6
df_split2[2] # 7, 8, 9
df_split2[3] # 10, 11
Hope I'm right, and that this is useful.
np.split(df, N)
function please. – Pandybat