从长度不等的嵌套列表中创建 pandas 数据框 [英] Create a pandas dataframe from a nested lists of unequal lengths
本文介绍了从长度不等的嵌套列表中创建 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有如下列表:
aa = ['aa1', 'aa2', 'aa3', 'aa4', 'aa5']
bb = ['bb1', 'bb2', 'bb3', 'bb4']
cc = ['cc1', 'cc2', 'cc3']
然后将其创建到嵌套列表中:
Which is then created into a nested list:
nest = [aa, bb, cc]
我要创建一个数据框,如下所示:
I want to create a dataframe as follows:
aa bb cc
aa1 bb1 cc1
aa2 bb2 cc2
aa3 bb3 cc3
aa4 bb4 nan
aa5 nan nan
我尝试过:
pd.DataFrame(nest, columns=['aa', 'bb', cc'])
但是结果是,每个列表都被写成一行(而不是一列)
But results is such that, each list is being written as a row (as opposed to a column)
推荐答案
itertools
中的zip_longest
函数执行以下操作:
The zip_longest
function from itertools
does this:
>>> import itertools, pandas
>>> pandas.DataFrame((_ for _ in itertools.zip_longest(*nest)), columns=['aa', 'bb', 'cc'])
aa bb cc
0 aa1 bb1 cc1
1 aa2 bb2 cc2
2 aa3 bb3 cc3
3 aa4 bb4 None
4 aa5 None None
如果您的熊猫版本较旧,则可能需要将zip_longest
包装在列表构造函数中.在较旧的Python上,您可能需要调用izip_longest
而不是zip_longest
.
If you have an older version of pandas you may need to wrap zip_longest
in a list constructor. On older Python you may need to call izip_longest
instead of zip_longest
.
这篇关于从长度不等的嵌套列表中创建 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文