2016-04-20 21 views

Antwort

1
def split_dataframe(df, size): 

    # size of each row 
    row_size = df.memory_usage().sum()/len(df) 

    # maximum number of rows of each segment 
    row_limit = size // row_size 

    # number of segments 
    seg_num = (len(df) + row_limit - 1) // row_limit 

    # split df 
    segments = [df.iloc[i*row_limit : (i+1)*row_limit] for i in range(seg_num)] 

    return segments 
+0

Ihre Lösung ist generisch und akzeptiert! – Segmented

0

Der einfachste Weg, dies zu tun, ist, wenn die Spalten des Datenrahmens konsistente Datentypen sind (d. H. Keine Objekte). Hier ist ein Beispiel, wie Sie damit umgehen könnten.

import numpy as np 
import pandas as pd 
from __future__ import division 

df = pd.DataFrame({'a': [1]*100, 'b': [1.1, 2] * 50, 'c': range(100)}) 

# calculate the number of bytes a row occupies 
row_bytes = df.dtypes.apply(lambda x: x.itemsize).sum() 

mem_limit = 1024 

# get the maximum number of rows in a segment 
max_rows = mem_limit/row_bytes 

# get the number of dataframes after splitting 
n_dfs = np.ceil(df.shape[0]/max_rows) 

# get the indices of the dataframe segments 
df_segments = np.array_split(df.index, n_dfs) 

# create a list of dataframes that are below mem_limit 
split_dfs = [df.loc[seg, :] for seg in df_segments] 

split_dfs 

Auch, wenn Sie durch Spalten anstelle von Zeilen aufteilen, hat Pandas eine handliche memory_usage Methode.