Home > Blockchain >  Split large dataframe in python by its columns
Split large dataframe in python by its columns

Time:01-22

Hey I'm looking for a solution to split a dataset (dataframe) by its columns in equal parts.

df: 400 rows × 3840 columns

I want to have 16 datasets in the end (400*240).

I tried to iterate with iloc in pandas, but its getting messy...

The result should look like this: df_1 [:, 1:240] df_2 [;, 241:480]
... df_16 [;, 3600:3840]

Thanks!

CodePudding user response:

Using a dictionary makes it easier :

dic_df = {}
for i in range(240):
   dic_df[i] = df[df.columns[16*i:16*(i 1)]]

CodePudding user response:

You can use numpy array_split for that. There you can specify in how many arrays an input array should be split. If the input array does not divide equally, you will get back one slightly bigger array and n-1/identically sized ones with n being the number of arrays you need. You can use the axis argument to specify along where to split your input. Here for the documentation.

  •  Tags:  
  • Related