2015-10-22 237 views
2

我有一個1,150,000行和6列的大型數據框。按行分割大型數據幀兩個多個數據幀

如何將數據幀分成5個數據幀,每個數據幀200,000行(最後一行是150,000行)?

+0

的可能的複製[?你怎麼分割成列表在Python均勻大小的塊(http://stackoverflow.com/questions/312443/how-do-你分割-A-列表進入,大小均勻,含塊狀物的蟒蛇) – MatsLindh

回答

0

使用列表理解來創建6個數據框的列表,然後可以將其分配給單獨的變量。

n = 200000 
list_df = [df[i:i+n] for i in range(0,df.shape[0],n)] 

輸出:

In [3]: df = pd.DataFrame(index=np.arange(1150000),data=np.random.rand(1150000,6 
    ...:)) 

In [4]: n = 200000 

In [5]: df1 = [df[i:i+n] for i in range(0,len(df),n)] 
In [6]: df1[0].shape 
Out[6]: (200000, 6) 

In [7]: df1[1].shape 
Out[7]: (200000, 6) 

In [8]: df1[2].shape 
Out[8]: (200000, 6) 

In [9]: df1[3].shape 
Out[9]: (200000, 6) 

In [10]: df1[4].shape 
Out[10]: (200000, 6) 

In [11]: df1[5].shape 
Out[11]: (150000, 6)