【问题标题】:Nan results when concatenating dataframes of different indices连接不同索引的数据帧时的 Nan 结果
【发布时间】:2017-05-09 20:55:44
【问题描述】:

我有 3 个数据帧我想连接维护 df1 的索引。

df1:                      
       sq/ft               

R     0.026715            
A    -0.016556            
P     0.007440      
V     0.009841 


 df2:  
       ml/sq

0     0.032394
1     0.065046
2     0.009021
3     0.041041

df3:
       mlsq/rt
0     0.000871
1    -0.000148
2     0.000484
3     0.026715  

想要的输出应该是这样的:

       sq/ft         ml/sq     mlsq/rt           

R     0.026715     0.032394   0.000871       
A    -0.016556     0.065046  -0.000148
P     0.007440     0.009021   0.000484
V     0.009841     0.041041   0.026715  

这是我在 pandas doc 之后尝试过的: http://pandas.pydata.org/pandas-docs/stable/merging.html#set-logic-on-the-other-axes

print(pd.concat([df2,df1,df3], axis=1, join_axes=[df1.index]))

其中 3 个是 df,我当前的输出是

      ml/sq         sq/ft

R      NaN       0.026715
A      NaN      -0.016556
P      NaN       0.007440
V      NaN       0.009841

df3 它甚至没有作为输出返回。

【问题讨论】:

    标签: pandas


    【解决方案1】:

    我认为您需要对齐索引 - 最简单的方法是在 df2.indexdf3.index 中设置索引 df1.index

    df2.index = df1.index
    df3.index = df1.index
    print(pd.concat([df2,df1,df3], axis=1))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-12
      • 2017-09-04
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多