Pandas: 将系列添加到具有不同索引的 DataFrame 时,系列会变成所有 NaN

创建于 2011-12-06  ·  9评论  ·  资料来源: pandas-dev/pandas

案例:

>>> df
               RP/Rsum  P.value
ID                             
A_23_P42353    17.8     0      
A_23_P369994   15.91    0      
A_33_P3262440  436.7    0.0005 
A_32_P199429   18.97    0      
A_23_P256724   22.24    0      
A_33_P3394689  24.24    0      
A_33_P3403117  27.14    0      
A_24_P252364   28.56    0      
A_23_P99515    31.82    0      
A_24_P261750   31.46    0 

>>> df.dtypes
RP/Rsum    float64
P.value    float64

>>> ids = pandas.Series(['51513', '9201', np.nan, np.nan, '8794', '6530', '7025', '4897', '84935', '11081'])
>>> df["test"] = ids
>>> df
               RP/Rsum  P.value  test
ID                                   
A_23_P42353    17.8     0        NaN 
A_23_P369994   15.91    0        NaN 
A_33_P3262440  436.7    0.0005   NaN 
A_32_P199429   18.97    0        NaN 
A_23_P256724   22.24    0        NaN 
A_33_P3394689  24.24    0        NaN 
A_33_P3403117  27.14    0        NaN 
A_24_P252364   28.56    0        NaN 
A_23_P99515    31.82    0        NaN 
A_24_P261750   31.46    0        NaN 
>>> df.dtypes
RP/Rsum    float64
P.value    float64
test       object

这也发生在浮动对象等上。 我不确定触发器是什么。

最有用的评论

当您不提供一个时,系列会被赋予一个隐含的 0, ..., N-1 索引——所以这正是我所期望的行为。 如果data是原始 ndarray 或列表,则不会发生这种情况。 所以当你这样做时:

df[col] = series

并且它使系列完全符合df的索引,这是一个功能而不是一个错误:) 所以

df['test'] = ids.values

在您的示例中可以正常工作

所有9条评论

我想知道它是否与我今天早上发现的这个问题有关:

>>> df = pandas.DataFrame(index=[1,2,3,4])
>>> df["test"] = pandas.Series(["B", "fdf", "344", np.nan])
>>> df["test2"] = ["B", "fdf", "344", np.nan]
>>> df   test  test2
1  fdf   B    
2  344   fdf  
3  NaN   344  
4  NaN   nan  

对我来说看起来像是某种逐一错误。

进一步挖掘导致在将项目设置为罪魁祸首时调用Series.reindex

>>> data 
0    B
1    fdf
2    344
3    NaN

>>>  df.index = ["A", "B", "C", "D"]
>>> data.reindex(df.index).values
array([nan, nan, nan, nan], dtype=object)

更多的挖掘导致reindex被调用的 index 属性给出了一个奇怪的结果:

>>> data.index.reindex(df.index)
(Index([A, B, C, D], dtype=object), array([-1, -1, -1, -1], dtype=int32))

然后将这些 -1 转换为 NaN。

用更正确的描述更新了错误标题。

当您不提供一个时,系列会被赋予一个隐含的 0, ..., N-1 索引——所以这正是我所期望的行为。 如果data是原始 ndarray 或列表,则不会发生这种情况。 所以当你这样做时:

df[col] = series

并且它使系列完全符合df的索引,这是一个功能而不是一个错误:) 所以

df['test'] = ids.values

在您的示例中可以正常工作

在这种情况下,如果还没有,也许应该在某处记录它。 同时我会按照你的建议调整我自己的代码,谢谢。

http://pandas.sourceforge.net/dsintro.html#column -selection-addition-deletion

When inserting a Series that does not have the same index as the DataFrame, it will be conformed to the DataFrame’s index:

In [180]: df['one_trunc'] = df['one'][:2]

In [181]: df
Out[181]: 
   one  flag   foo  one_trunc
a  1    False  bar  1        
b  2    False  bar  2        
c  3    True   bar  NaN      
d  NaN  False  bar  NaN      

You can insert raw ndarrays but their length must match the length of the DataFrame’s index.

当插入与 DataFrame 没有相同索引的 Series 时,它将符合 DataFrame 的索引这一事实背后的想法是什么?

从系列创建 DataFrame 时,生成的索引涵盖所有单独的系列索引。 那么为什么当 df['new_column'] = series 时不使用这个想法呢?
所以你尝试添加数据,但忽略所有与 DataFrame 索引不匹配的值?
如果 _index extension_ 存在,当不想扩展索引(当前行为)时,总是可以执行 df['new_column'] = series.reindex(df.index) ?

In [256]: df = pandas.DataFrame({'A': pandas.Series(['foo', 'bar'], index=['a', 'b']),
   .....:                        'B': pandas.Series([10, 20], index=['b', 'c'])})

In [257]: df
Out[257]:
   A    B
a  foo  NaN
b  bar  10.000
c  NaN  20.000

In [258]: df['C'] = pandas.Series(range(3), index=['a', 'c', 'd'])

In [259]: df
Out[259]:
   A    B       C
a  foo  NaN     0.000
b  bar  10.000  NaN
c  NaN  20.000  1.000

在上面的示例中,我希望 DataFrame 中有一行“d”。

好吧,我认为基本的想法是 DataFrame 是“系列的固定长度的类似字典的容器”。 当您使用没有显式索引的 Series dict 构造 DataFrame 时,除了它们的联合之外,没有明显的索引。

我可以看到隐式扩展索引的论点,但无论哪种方式都有权衡

此页面是否有帮助?
0 / 5 - 0 等级