从不等长的嵌套列表创建一个 pandas 数据框

Create a pandas dataframe from a nested lists of unequal lengths(从不等长的嵌套列表创建一个 pandas 数据框)

本文介绍了从不等长的嵌套列表创建一个 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个列表如下:

aa = ['aa1', 'aa2', 'aa3', 'aa4', 'aa5']bb = ['bb1', 'bb2', 'bb3', 'bb4']cc = ['cc1', 'cc2', 'cc3']

然后将其创建为嵌套列表:

nest = [aa, bb, cc]

我想按如下方式创建一个数据框:

aa bb ccaa1 bb1 cc1aa2 bb2 cc2aa3 bb3 cc3aa4 bb4 南aa5 南南

我试过了:

pd.DataFrame(nest, columns=['aa', 'bb', cc'])

但结果是,每个列表都被写成一行(而不是一列)

解决方案

itertools 中的 zip_longest 函数是这样做的:

<预><代码>>>>导入 itertools、pandas>>>pandas.DataFrame((_ for _ in itertools.zip_longest(*nest)), columns=['aa', 'bb', 'cc'])aa bb cc0 aa1 bb1 cc11 aa2 bb2 cc22 aa3 bb3 cc33 aa4 bb4 无4 aa5 无 无

如果您有旧版本的熊猫,您可能需要将 zip_longest 包装在列表构造函数中.在较旧的 Python 上,您可能需要调用 izip_longest 而不是 zip_longest.

So I have a list as follows:

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

I've tried:

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)

解决方案

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

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 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从不等长的嵌套列表创建一个 pandas 数据框

基础教程推荐