def split_df(df, nums):
'''
reverse operation:
pd.concat(dfs)
'''
rows = df.shape[0]
interval = round(rows / nums)
dfs = []
for i in range(nums):
s = i * interval
if i != nums - 1:
e = (i + 1) * interval
else:
e = rows
dfs.append(df.iloc[s:e, :])
return dfs
def split_list(lst, nums):
rows = len(lst)
interval = round(rows / nums)
lsts = []
for i in range(nums):
s = i * interval
if i != nums - 1:
e = (i + 1) * interval
else:
e = rows
lsts.append(lst[s:e])
while [] in lsts:
lsts.remove([])
return lsts
突然发现了一个更好的方法
def get_chunks(iterable, chunks=1):
# This is from http://stackoverflow.com/a/2136090/2073595
lst = list(iterable)
return [lst[i::chunks] for i in range(chunks)]