Pandas - fragment of a large data array in pieces

I have a large data frame (> 3MM rows) that I am trying to pass through a function (one of them is basically simplified), and I continue to receive the message Memory Error.

I think I pass too much of the data into a function, so I try:

1) Slice the data block into smaller pieces (preferably chopped AcctName)

2) Pass the data frame to the function

3) Combining data into one large data frame

def trans_times_2(df):
    df['Double_Transaction'] = df['Transaction'] * 2

large_df 
AcctName   Timestamp    Transaction
ABC        12/1         12.12
ABC        12/2         20.89
ABC        12/3         51.93    
DEF        12/2         13.12
DEF        12/8          9.93
DEF        12/9         92.09
GHI        12/1         14.33
GHI        12/6         21.99
GHI        12/12        98.81

I know that my function works correctly as it will work on a smaller data frame (e.g. 40,000 rows). I tried the following, but I was not able to compose small data frames into one large frame.

def split_df(df):
    new_df = []
    AcctNames = df.AcctName.unique()
    DataFrameDict = {elem: pd.DataFrame for elem in AcctNames}
    key_list = [k for k in DataFrameDict.keys()]
    new_df = []
    for key in DataFrameDict.keys():
        DataFrameDict[key] = df[:][df.AcctNames == key]
        trans_times_2(DataFrameDict[key])
    rejoined_df = pd.concat(new_df)

How I view data sharing:

df1
AcctName   Timestamp    Transaction  Double_Transaction
ABC        12/1         12.12        24.24
ABC        12/2         20.89        41.78
ABC        12/3         51.93        103.86

df2
AcctName   Timestamp    Transaction  Double_Transaction
DEF        12/2         13.12        26.24
DEF        12/8          9.93        19.86
DEF        12/9         92.09        184.18

df3
AcctName   Timestamp    Transaction  Double_Transaction
GHI        12/1         14.33        28.66
GHI        12/6         21.99        43.98
GHI        12/12        98.81        197.62
+3
1

, , .

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

:

list_df[0]
list_df[1]
etc...

, pd.concat.

AcctName

list_df = []

for n,g in df.groupby('AcctName'):
    list_df.append(g)
+7

Source: https://habr.com/ru/post/1682222/


All Articles