在 Pandas Dataframe pd.concat 之后,我得到了 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22531164/
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 21:49:51  来源:igfitidea点击:

After Pandas Dataframe pd.concat I get NaNs

pythonpandasdataframeconcat

提问by prre72

I have three pandas df one of them has been 'row'-shifted and the first element is empty. When I concatenate the three df to obtain a single 3-column dataframe I get all NaN in two out of three columns:

我有三只Pandas df 其中一个已被“行”移动,第一个元素为空。当我连接三个 df 以获得单个 3 列数据帧时,我在三列中的两列中获得所有 NaN:

df1:

df1:

                    S
2010-12-31         True
2011-01-01        False
2011-01-02        False

df2:

df2:

               P
2010-12-31           
2011-01-01    On
2011-01-02    On

df3:

df3:

              C
2010-12-31    On
2011-01-01    On
2011-01-02    On

res = pd.concat([df1, df2, df3]):

res = pd.concat([df1, df2, df3]):

                    P         C           S
2010-12-31        NaN        NaN         True
2011-01-01        NaN        NaN        False
2011-01-02        NaN        NaN        False

The order seems to be inverted as well...

顺序好像也颠倒了……

Many thanks

非常感谢

回答by unutbu

In [2]: index = pd.DatetimeIndex(['2010-12-31', '2011-01-01', '2011-01-02'])

In [3]: df1 = pd.DataFrame({'S':[True,False,False]}, index=index)

In [4]: df2 = pd.DataFrame({'P':['','On','On']}, index=index)

In [5]: df3 = pd.DataFrame({'C':['On','On','On']}, index=index)

If your DataFrames are defined as above, then pd.concatwith axis=1should work:

如果您的 DataFrame 定义如上,那么pd.concatwithaxis=1应该可以工作:

In [7]: pd.concat([df1,df2,df3], axis=1)
Out[7]: 
                S   P   C
2010-12-31   True      On
2011-01-01  False  On  On
2011-01-02  False  On  On

[3 rows x 3 columns]