在Pandas Dataframe pd.concat之后,我得到NaNs [英] After Pandas Dataframe pd.concat I get NaNs
本文介绍了在Pandas Dataframe pd.concat之后,我得到NaNs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我连接三个df以获得一个单列的3列数据框时,我得到所有的NaN在三列中的两列:
df1:
S
2010-12-31 True
2011-01-01 False
2011-01-02假
df2:
P
2010-12-31
2011-01-01在
2011-01-02在
df3:
C
2010-12- 31 On
2011-01-01在
2011-01-02在
res = pd.concat([df1,df2,df3]):
PCS
2010-12-31 NaN NaN True
2011-01-01 NaN NaN False
2011-01-02 NaN NaN False
订单似乎也是颠倒的...
非常感谢
在[3]中:df1 = pd.DataFrame({'S':[True,False,False]},index = index)
在[4]中:df2 = pd.DataFrame({'P':['','On','On']},index = index)
在[5] df3 = pd.DataFrame({'C':['On','On','On']},index = index)
如果您的DataFrames定义如上,那么 pd.concat
与 axis = 1
应该工作:
在[7]中:pd.concat([df1,df2,df3],axis = 1)
输出[7]:
SPC
2010-12-31 True On
2011-01-01 False On On
2011-01-02 False On On
[3行×3列]
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:
df1:
S
2010-12-31 True
2011-01-01 False
2011-01-02 False
df2:
P
2010-12-31
2011-01-01 On
2011-01-02 On
df3:
C
2010-12-31 On
2011-01-01 On
2011-01-02 On
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
解决方案
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.concat
with axis=1
should work:
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]
这篇关于在Pandas Dataframe pd.concat之后,我得到NaNs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文