Memory issues when using pandas for a huge dataset

Viewed 41

I have about dataset of N=10^9 pairs of integers. Each integer is nonnegative and less than 10^6. They are like

2 3 
2 4 
3 6
4 6
...

I need to group the data by one of the columns. Quite new to Pandas, I tried the following code but got a memory issue. Pandas seems to take huge amount of memory and then the code got killed (on a laptop with 16 GB memory. Any work-around?

import numpy as np
import pandas as pd
import time


N=10**9


df = pd.DataFrame({
    "A": np.random.randint(0, N, N),
    "B": np.random.randint(0, N, N)
})

starttime=time.time()
df.groupby("B")["A"].agg(list)
print (df)
print("Demo: it took this long to run pandas with N= {}, time={}".format(N, time.time()-starttime))
0 Answers
Related