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

如何从不等长列表的字典创建DataFrame并截断为特定长度?

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

如何从不等长列表的字典创建DataFrame并截断为特定长度?

您可以过滤

values
dict
dictcomprehension
,那么
Dataframe
完美的作品:

print ({k:v[:min_length] for k,v in data_dict.items()}){'b': [1, 2, 3], 'c': [2, 45, 67], 'a': [1, 2, 3]}df = pd.Dataframe({k:v[:min_length] for k,v in data_dict.items()})print (df)   a  b   c0  1  1   21  2  2  452  3  3  67

如果可能的话,一些长度可以小于

min_length
添加
Series

data_dict = {'a': [1,2,3,4], 'b': [1,2], 'c': [2,45,67,93,82,92]}min_length = 3df = pd.Dataframe({k:pd.Series(v[:min_length]) for k,v in data_dict.items()})print (df)   a    b   c0  1  1.0   21  2  2.0  452  3  NaN  67

时间

In [355]: %timeit (pd.Dataframe({k:v[:min_length] for k,v in data_dict.items()}))The slowest run took 5.32 times longer than the fastest. This could mean that an intermediate result is being cached.1000 loops, best of 3: 520 µs per loopIn [356]: %timeit (pd.Dataframe({k:pd.Series(v[:min_length]) for k,v in data_dict.items()}))The slowest run took 4.50 times longer than the fastest. This could mean that an intermediate result is being cached.1000 loops, best of 3: 937 µs per loop#Allen's solutionIn [357]: %timeit (pd.Dataframe.from_dict(data_dict,orient='index').T.dropna())1 loop, best of 3: 16.7 s per loop

计时代码

np.random.seed(123)L = list('ABCDEFGH')N = 500000min_length = 10000data_dict = {k:np.random.randint(10, size=np.random.randint(N)) for k in L}


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

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

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