栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python-将数据框拆分为多个数据框

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python-将数据框拆分为多个数据框

首先,你的方法效率低下,因为在没有足够的空间容纳新条目的情况下,由于必须定期增长列表,因此逐行追加到列表的速度很慢,因此列表大小在此方面会更好,因为前面并分配一次。

但是,我认为从根本上讲,你的方法有点浪费,因为你已经有了一个数据框,为什么要为这些用户中的每一个创建一个新的?

我将按列对数据帧进行排序

'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'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/391488.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号