首先,你的方法效率低下,因为在没有足够的空间容纳新条目的情况下,由于必须定期增长列表,因此逐行追加到列表的速度很慢,因此列表大小在此方面会更好,因为前面并分配一次。
但是,我认为从根本上讲,你的方法有点浪费,因为你已经有了一个数据框,为什么要为这些用户中的每一个创建一个新的?
我将按列对数据帧进行排序
'name',将索引设置为此,如果需要,则不要删除该列。
然后生成所有唯一条目的列表,然后你可以使用这些条目执行查找,并且至关重要的是,如果仅查询数据,请使用选择条件返回数据框上的视图,而不会产生昂贵的数据副本。
所以:
# sort the dataframedf.sort(columns=['name'], inplace=True)# set the index to be this and don't dropdf.set_index(keys=['name'], drop=False,inplace=True)# get a list of namesnames=df['name'].unique().tolist()# now we can perform a lookup on a 'view' of the dataframejoe = df.loc[df.name=='joe']# now you can query all 'joes'
编辑
sort现在已弃用,你需要立即使用
sort_values:
# sort the dataframedf.sort_values(by='name', axis=1, inplace=True)# set the index to be this and don't dropdf.set_index(keys=['name'], drop=False,inplace=True)# get a list of namesnames=df['name'].unique().tolist()# now we can perform a lookup on a 'view' of the dataframejoe = df.loc[df.name=='joe']# now you can query all 'joes'



