split a pd dataframe

Solutions on MaxInterview for split a pd dataframe by the best coders in the world

showing results for - "split a pd dataframe"
Nathael
10 Mar 2018
1In [255]: df = pd.DataFrame(np.random.rand(5, 6), columns=list('abcdef'))
2
3In [256]: df
4Out[256]:
5          a         b         c         d         e         f
60  0.823638  0.767999  0.460358  0.034578  0.592420  0.776803
71  0.344320  0.754412  0.274944  0.545039  0.031752  0.784564
82  0.238826  0.610893  0.861127  0.189441  0.294646  0.557034
93  0.478562  0.571750  0.116209  0.534039  0.869545  0.855520
104  0.130601  0.678583  0.157052  0.899672  0.093976  0.268974
11
12In [257]: dfs = np.split(df, [4], axis=1)
13
14In [258]: dfs[0]
15Out[258]:
16          a         b         c         d
170  0.823638  0.767999  0.460358  0.034578
181  0.344320  0.754412  0.274944  0.545039
192  0.238826  0.610893  0.861127  0.189441
203  0.478562  0.571750  0.116209  0.534039
214  0.130601  0.678583  0.157052  0.899672
22
23In [259]: dfs[1]
24Out[259]:
25          e         f
260  0.592420  0.776803
271  0.031752  0.784564
282  0.294646  0.557034
293  0.869545  0.855520
304  0.093976  0.268974