1

I have a dataframe df, containing only one column 'Info', which I want to split into multiple dataframes based on a list of indices, ls = [23,76,90,460,790]. If I want to use np.array_split(), how do I pass the list so that it parses the data from these indices with each index being the first row of split dataframes.

ABC
  • 45
  • 3

1 Answers1

-1

I don't think you can use np.array_split() here (you can access the underlying .values of the primary DF but you'd get back numpy arrays - not DFs...) - what you can do is use .iloc and "slice" from your DF, eg:

from itertools import zip_longest

dfs = [df.iloc[s: e] for s, e in zip_longest(ls[::2], ls[1::2])]
Jon Clements
  • 138,671
  • 33
  • 247
  • 280