我有一个数据框,大约有1亿行(内存中占用1.4Gb)
给定输入:
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
我想获取每个id的第一行索引。示例:
Out[1]:
id first_idx
0 1 0
1 2 2
2 3 7
2 4 13
我的当前方法非常慢:
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
任何建议都将是巨大的帮助。