我有两个以相同格式编制索引的序列下面是这两个片段(由于数据的大小,我不会显示整个集合):

>>> s1
Out[52]:
parameter_id  parameter_type_cs_id
4959          1                        -0.2664122
4960          1                      -0.004289398
4961          1                      -0.006652875
4966          1                      -0.004208685
4967          1                       -0.02268688
4968          1                       -0.05958452
4969          1                       -0.01133198
4970          1                       -0.01968251
4972          1                       -0.05860331
4974          1                       -0.08260008
4975          1                       -0.05402012
4979          1                        -0.0308407
4980          1                       -0.02232495
4987          1                        -0.2315813
4990          1                       -0.02171027
...
727241        1                            -0.00156766
727242        1                          -0.0009964491
727243        1                           -0.007068732
727244        1                           -0.003500738
727245        1                           -0.006572505
727246        1                          -0.0005814131
728060        1                             -0.0144799
728062        1                             -0.0418521
728063        1                            -0.01367948
728065        1                            -0.03625054
728066        1                            -0.06806824
728068        1                           -0.007910916
728071        1                           -0.005482052
728073        1                           -0.005845178
intercept                             [-11.4551819018]
Name: coef, Length: 1529, dtype: object

>>> s2
Out[53]:
parameter_id  parameter_type_cs_id
4958          1                       -0.001683882
4959          1                          -1.009859
4960          1                      -0.0004456379
4961          1                       -0.005564386
4963          1                         -0.9145955
4964          1                      -0.0009077246
4965          1                      -0.0003179153
4966          1                      -0.0006907124
4967          1                        -0.02125838
4968          1                        -0.02443978
4969          1                       -0.002665334
4970          1                       -0.003135213
4971          1                      -0.0003539563
4972          1                        -0.03684852
4973          1                      -0.0001203596
...
728044        1                          -0.0003084855
728060        1                              -0.925618
728061        1                           -0.001192743
728062        1                             -0.9203911
728063        1                           -0.002522615
728064        1                          -0.0003572484
728065        1                           -0.003475959
728066        1                            -0.02329697
728068        1                           -0.001412785
728069        1                           -0.002095895
728070        1                          -9.790675e-05
728071        1                          -0.0003013977
728072        1                          -0.0003369116
728073        1                           -0.000249748
intercept                             [-12.1281459287]
Name: coef, Length: 1898, dtype: object

索引格式是相同的,因此我尝试将它们放入数据帧中,如下所示:
d = {'s1': s1, 's2': s2}
df = pd.DataFrame(d)

但是我注意到输出几乎都NaN,这让我感到震惊。我查看了各个序列的索引,注意到dataframe将它们作为字符串而不是与序列相同的格式
>>> s1.index.values
Out[54]:
array([(4959, 1), (4960, 1), (4961, 1), ..., (728071, 1), (728073, 1),
       ('intercept', '')], dtype=object)

>>> s2.index.values
Out[55]:
array([(4958, 1), (4959, 1), (4960, 1), ..., (728072, 1), (728073, 1),
       ('intercept', '')], dtype=object)

但数据帧有字符串
>>> df.index.values
Out[56]:
array([('4959', '1'), ('4960', '1'), ('4961', '1'), ..., ('8666', '1'),
       ('9638', '1'), ('intercept', '')], dtype=object)

为什么它会改变类型,这会导致我的问题。。。?
对我来说,更奇怪的是,如果我在一个较小的集合上尝试与上面相同的操作,我会看到预期的行为(不是所有的NaN并且索引都没有转换)
s1_ = s1[:15]
s2_ = s2[:15]
d_ = {'s1': s1_, 's2': s2_}
df_ = pd.DataFrame(d_) #<---- This has the behavior I would expect

编辑
我已经找到了一种可行的方法,但我不确定它为什么会这样工作,如果我将两个序列转换为数据帧,然后将它们连接起来,它将按预期工作:
df_1 = pd.DataFrame({'s1': s1})
df_2 = pd.DataFrame({'s2': s2})
new_df = df_1.join(df_2) #WHY DOES THIS WAY WORK!?!?

最佳答案

它将索引转换为字符串的原因是

intercept                             [-11.4551819018]

在您的序列中,数据是一个字符串大熊猫数据帧的文档说明,当从一个系列中构建数据帧时,数据帧保持相同的索引,因为数据中的最后一行,该序列导致转换到所有字符串。
创建两个数据帧然后加入它们的方法是可行的,因为索引是一致的,因为您使用的是相同的数据结构(例如数据帧),而不是从一个数据结构(序列)转换为另一个数据结构(数据帧)。这似乎是熊猫特有的东西。我会坚持你的解决方案。

10-08 04:15