I have a dataframe with roughly 100M rows, (1.4Gb in memory)
Given the input:
df.head()
Out[1]:
id term x
0 1 A 3
1 1 B 2
2 2 A 1
3 2 B 1
4 2 F 1
5 2 G 1
6 2 Z 1
7 3 K 1
8 3 M 1
9 3 N 1
10 3 Q 1
11 3 R 1
12 3 Z 1
13 4 F 1
I'd like to retrieve the index of the first row for each id. Example:
Out[1]:
id first_idx
0 1 0
1 2 2
2 3 7
2 4 13
My current approach is incredibly slow:
first_row = {}
last_id = None
first_row = None
#iterate over all rows
for idx,r in bow.iterrows():
cid = r['id']
if cid != last_id: #is this an ID we haven't seen before?
first_row[cid] = idx
last_id = cid
Any advice would be a huge help.