Package: a-pandas-ex-split
Several methods to split a pandas DataFrame/Seriespipinstalla-pandas-ex-splitfroma_pandas_ex_splitimportpd_add_df_splitimportpandasaspdpd_add_df_split()df=pd.read_csv("https://raw.githubusercontent.com/pandas-dev/pandas/main/doc/data/titanic.csv")df=df[:50]t1=df.ds_iloc_split(splitindex=[10,20,40])print(f"\n\n{t1=}")t2=df.ds_loc_split(splitindex=[10,20,35])print(f"\n\n{t2=}")t3=df.ds_iloc_split_pairwise(splitindex=[(0,10),(25,30)],include_last=True)print(f"\n\n{t3=}")t4=df.ds_split_in_n_parts(n=9)# len of results = [6, 6, 6, 6, 6, 5, 5, 5, 5]print(f"\n\n{t4=}")t5=df.ds_split_in_n_parts_of_length(size_of_each=8,exact_split=False)# len of results = [9, 9, 8, 8, 8, 8]print(f"\n\n{t5=}")t6=df.ds_split_in_n_parts_of_length(size_of_each=8,exact_split=True)# len of results = [8, 8, 8, 8, 8, 8, 2]print(f"\n\n{t6=}")t7=df.PassengerId.ds_split_in_n_parts_of_length(size_of_each=8,exact_split=True)# len of results = [8, 8, 8, 8, 8, 8, 2]print(f"\n\n{t7=}")t1=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ6701...51.8625E46S7803...21.0750NaNS8913...11.1333NaNS91012...30.0708NaNC[10rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked101113...16.7000G6S111211...26.5500C103S121303...8.0500NaNS131403...31.2750NaNS141503...7.8542NaNS151612...16.0000NaNS161703...29.1250NaNQ171812...13.0000NaNS181903...18.0000NaNS192013...7.2250NaNC[10rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked202102...26.0000NaNS212212...13.0000D56S222313...8.0292NaNQ232411...35.5000A6S242503...21.0750NaNS252613...31.3875NaNS262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS303101...27.7208NaNC313211...146.5208B78C323313...7.7500NaNQ333402...10.5000NaNS343501...82.1708NaNC353601...52.0000NaNS363713...7.2292NaNC373803...8.0500NaNS383903...18.0000NaNS394013...11.2417NaNC[20rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked404103...9.4750NaNS414202...21.0000NaNS424303...7.8958NaNC434412...41.5792NaNC444513...7.8792NaNQ454603...8.0500NaNS464703...15.5000NaNQ474813...7.7500NaNQ484903...21.6792NaNC495003...17.8000NaNS[10rowsx12columns]]t2=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ6701...51.8625E46S7803...21.0750NaNS8913...11.1333NaNS91012...30.0708NaNC101113...16.7000G6S[11rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked101113...16.7000G6S111211...26.5500C103S121303...8.0500NaNS131403...31.2750NaNS141503...7.8542NaNS151612...16.0000NaNS161703...29.1250NaNQ171812...13.0000NaNS181903...18.0000NaNS192013...7.2250NaNC202102...26.0000NaNS[11rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked202102...26.0000NaNS212212...13.0000D56S222313...8.0292NaNQ232411...35.5000A6S242503...21.0750NaNS252613...31.3875NaNS262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS303101...27.7208NaNC313211...146.5208B78C323313...7.7500NaNQ333402...10.5000NaNS343501...82.1708NaNC353601...52.0000NaNS[16rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked353601...52.0000NaNS363713...7.2292NaNC373803...8.0500NaNS383903...18.0000NaNS394013...11.2417NaNC404103...9.4750NaNS414202...21.0000NaNS424303...7.8958NaNC434412...41.5792NaNC444513...7.8792NaNQ454603...8.0500NaNS464703...15.5000NaNQ474813...7.7500NaNQ484903...21.6792NaNC495003...17.8000NaNS[15rowsx12columns]]t3=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ6701...51.8625E46S7803...21.0750NaNS8913...11.1333NaNS91012...30.0708NaNC101113...16.7000G6S[11rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked252613...31.3875NaNS262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS303101...27.7208NaNC[6rowsx12columns]]t4=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ[6rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked6701...51.8625E46S7803...21.0750NaNS8913...11.1333NaNS91012...30.0708NaNC101113...16.7000G6S111211...26.5500C103S[6rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked121303...8.0500NaNS131403...31.2750NaNS141503...7.8542NaNS151612...16.0000NaNS161703...29.1250NaNQ171812...13.0000NaNS[6rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked181903...18.0000NaNS192013...7.2250NaNC202102...26.0000NaNS212212...13.0000D56S222313...8.0292NaNQ232411...35.5000A6S[6rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked242503...21.0750NaNS252613...31.3875NaNS262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS[6rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked303101...27.7208NaNC313211...146.5208B78C323313...7.7500NaNQ333402...10.5000NaNS343501...82.1708NaNC[5rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked353601...52.0000NaNS363713...7.2292NaNC373803...8.0500NaNS383903...18.0000NaNS394013...11.2417NaNC[5rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked404103...9.4750NaNS414202...21.0000NaNS424303...7.8958NaNC434412...41.5792NaNC444513...7.8792NaNQ[5rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked454603...8.0500NaNS464703...15.5000NaNQ474813...7.7500NaNQ484903...21.6792NaNC495003...17.8000NaNS[5rowsx12columns]]t5=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ6701...51.8625E46S7803...21.0750NaNS8913...11.1333NaNS[9rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked91012...30.0708NaNC101113...16.7000G6S111211...26.5500C103S121303...8.0500NaNS131403...31.2750NaNS141503...7.8542NaNS151612...16.0000NaNS161703...29.1250NaNQ171812...13.0000NaNS[9rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked181903...18.0000NaNS192013...7.2250NaNC202102...26.0000NaNS212212...13.0000D56S222313...8.0292NaNQ232411...35.5000A6S242503...21.0750NaNS252613...31.3875NaNS[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS303101...27.7208NaNC313211...146.5208B78C323313...7.7500NaNQ333402...10.5000NaNS[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked343501...82.1708NaNC353601...52.0000NaNS363713...7.2292NaNC373803...8.0500NaNS383903...18.0000NaNS394013...11.2417NaNC404103...9.4750NaNS414202...21.0000NaNS[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked424303...7.8958NaNC434412...41.5792NaNC444513...7.8792NaNQ454603...8.0500NaNS464703...15.5000NaNQ474813...7.7500NaNQ484903...21.6792NaNC495003...17.8000NaNS[8rowsx12columns]]t6=[PassengerIdSurvivedPclass...FareCabinEmbarked0103...7.2500NaNS1211...71.2833C85C2313...7.9250NaNS3411...53.1000C123S4503...8.0500NaNS5603...8.4583NaNQ6701...51.8625E46S7803...21.0750NaNS[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked8913...11.1333NaNS91012...30.0708NaNC101113...16.7000G6S111211...26.5500C103S121303...8.0500NaNS131403...31.2750NaNS141503...7.8542NaNS151612...16.0000NaNS[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked161703...29.1250NaNQ171812...13.0000NaNS181903...18.0000NaNS192013...7.2250NaNC202102...26.0000NaNS212212...13.0000D56S222313...8.0292NaNQ232411...35.5000A6S[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked242503...21.0750NaNS252613...31.3875NaNS262703...7.2250NaNC272801...263.0000C23C25C27S282913...7.8792NaNQ293003...7.8958NaNS303101...27.7208NaNC313211...146.5208B78C[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked323313...7.7500NaNQ333402...10.5000NaNS343501...82.1708NaNC353601...52.0000NaNS363713...7.2292NaNC373803...8.0500NaNS383903...18.0000NaNS394013...11.2417NaNC[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked404103...9.4750NaNS414202...21.0000NaNS424303...7.8958NaNC434412...41.5792NaNC444513...7.8792NaNQ454603...8.0500NaNS464703...15.5000NaNQ474813...7.7500NaNQ[8rowsx12columns],PassengerIdSurvivedPclass...FareCabinEmbarked484903...21.6792NaNC495003...17.8000NaNS[2rowsx12columns]]t7=[0112233445566778Name:PassengerId,dtype:int64,89910101111121213131414151516Name:PassengerId,dtype:int64,16171718181919202021212222232324Name:PassengerId,dtype:int64,24252526262727282829293030313132Name:PassengerId,dtype:int64,32333334343535363637373838393940Name:PassengerId,dtype:int64,40414142424343444445454646474748Name:PassengerId,dtype:int64,48494950Name:PassengerId,dtype:int64]