Gibt es eine Möglichkeit, einen Pandas-Datenrahmen in mehrere Datenrahmen aufzuteilen, die durch die Speichernutzung beschränkt sind?Pandas Dataframe geteilt durch Speichernutzung
0
A
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
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.
Verwandte Themen
- 1. Python/Pandas Dataframe ersetzen 0 durch Medianwert
- 2. Dataframe PANDAS
- 3. Flachen verschachtelten Pandas Dataframe
- 4. Dataframe Spaltendaten - Pandas - Python
- 5. Schnittfunktion geteilt durch Faktorstufen
- 6. Java String geteilt durch "|"
- 7. Pandas DataFrame Spaltenverkettung
- 8. Converting Zeitzone Pandas Dataframe
- 9. XML Pandas Dataframe
- 10. Serialisierung eines Pandas DataFrame
- 11. Pandas DataFrame Pivot-Problem
- 12. Anfügen Spalte Pandas Dataframe
- 13. Pandas Dataframe Spalt Inhalt
- 14. Pandas: Sampling eines DataFrame
- 15. Pandas Dataframe Spaltenauswahl
- 16. pandas DataFrame diagonal
- 17. Pandas DataFrame Leistung
- 18. Pandas Dataframe - Lookup Error
- 19. von Pandas Dataframe
- 20. Unmelt Pandas Dataframe
- 21. Abfrage auf Pandas Dataframe
- 22. Pandas DataFrame ApplyMap Methode
- 23. Schiebefenster über Pandas Dataframe
- 24. Pandas Dataframe-Unterselektion
- 25. Natürlich sortieren Pandas DataFrame
- 26. Pandas Dataframe groupby
- 27. Pandas: Attribute: 'Dataframe' Objekt
- 28. Pickle Dump Pandas DataFrame
- 29. Pandas Dataframe zu Excel Blatt
- 30. Wie man NaNs durch vorhergehende Werte in pandas DataFrame ersetzt?
Ihre Lösung ist generisch und akzeptiert! – Segmented