pandas 使用 .concat 创建熊猫数据框时包含空系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30518588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:24:22  来源:igfitidea点击:

Include empty series when creating a pandas dataframe with .concat

pythonpandasconcatenationmulti-index

提问by Alex

UPDATE: This is no longer an issue since at least pandas version 0.18.1. Concatenating empty series doesn't drop them anymore so this question is out of date.

更新:这不再是一个问题,因为至少大Pandas版本 0.18.1。连接空系列不再删除它们,所以这个问题已经过时了。

I want to create a pandas dataframe from a list of series using .concat. The problem is that when one of the series is empty it doesn't get included in the resulting dataframe but this makes the dataframe be the wrong dimensions when I then try to rename its columns with a multi-index. UPDATE: Here's an example...

我想使用 .concat 从系列列表中创建一个 Pandas 数据框。问题是,当其中一个系列为空时,它不会包含在结果数据框中,但是当我尝试使用多索引重命名其列时,这会使数据框成为错误的维度。更新:这是一个例子......

import pandas as pd

sers1 = pd.Series()
sers2 = pd.Series(['a', 'b', 'c'])
df1 = pd.concat([sers1, sers2], axis=1)

This produces the following dataframe:

这会产生以下数据帧:

>>> df1
0    a
1    b
2    c
dtype: object

But I want it to produce something like this:

但我希望它产生这样的东西:

>>> df2
    0  1
0 NaN  a
1 NaN  b
2 NaN  c

It does this if I put a single nan value anywhere in ser1 but it seems like this should be possible automatically even if some of my series are totally empty.

如果我在 ser1 中的任何位置放置一个 nan 值,它就会这样做,但即使我的某些系列完全为空,这似乎也应该是自动的。

回答by Alex

Passing an argument for levels will do the trick. Here's an example. First, the wrong way:

传递级别的参数就可以解决问题。这是一个例子。一、方法错误:

import pandas as pd
ser1 = pd.Series()
ser2 = pd.Series([1, 2, 3])
list_of_series = [ser1, ser2, ser1]
df = pd.concat(list_of_series, axis=1)

Which produces this:

产生这个:

>>> df
   0
0  1
1  2
2  3

But if we add some labels to the levels argument, it will include all the empty series too:

但是如果我们在 levels 参数中添加一些标签,它也会包含所有空系列:

import pandas as pd
ser1 = pd.Series()
ser2 = pd.Series([1, 2, 3])
list_of_series = [ser1, ser2, ser1]
labels = range(len(list_of_series))
df = pd.concat(list_of_series, levels=labels, axis=1)

Which produces the desired dataframe:

产生所需的数据帧:

>>> df
    0  1   2
0 NaN  1 NaN
1 NaN  2 NaN
2 NaN  3 NaN