我有一个dict
of lists
(其长度可变),我期待着一种从中创建数据框的有效方法。
假设我有最小列表长度,所以我可以在创建数据框时截断较大列表的大小。
这是我的虚拟代码
data_dict = {'a': [1,2,3,4], 'b': [1,2,3], 'c': [2,45,67,93,82,92]}
min_length = 3
我可以有一个包含 10k 或 20k 键的字典,因此寻找一种有效的方法来创建如下所示的 DataFrame
>>> df
a b c
0 1 1 2
1 2 2 45
2 3 3 67
单行解决方案:
#Construct the df horizontally and then transpose. Finally drop rows with nan.
pd.DataFrame.from_dict(data_dict,orient='index').T.dropna()
Out[326]:
a b c
0 1.0 1.0 2.0
1 2.0 2.0 45.0
2 3.0 3.0 67.0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)