【问题标题】:Assign a list with a missing value to a Pandas Series in Python将缺失值的列表分配给 Python 中的 Pandas 系列
【发布时间】:2016-03-09 18:57:26
【问题描述】:

当我尝试将缺少值 np.nan 的列表分配给 Pandas Series 以下是重现事实的代码。

import numpy as np
import pandas as pd
S = pd.Series(0, index = list('ABCDE'))
>>> S
A    0
B    0
C    0
D    0
E    0
dtype: int64

ind = [True, False, True, False, True]
x = [1, np.nan, 2]

>>> S[ind]
A    0
C    0
E    0
dtype: int64

将 x 分配给 S[ind]

S[ind] = x

S 中的某些东西

>>> S
A     1
B     0
C     2
D     0
E   NaN
dtype: float64

我期待S

>>> S
A     1
B     0
C     NaN
D     0
E     2
dtype: float64

谁能解释一下?

【问题讨论】:

  • 我得到了预期的输出,我使用的是 Ubuntu 14.04、Python 2.7.10、NumPy 1.10.1 和 Pandas 0.17.1。
  • 感谢您的回复。我正在使用 Python 2.6.6、NumPy 1.9.2 和 Pandas 0.16.0。我应该列出两个模块的版本。
  • 也许更新是个好主意。

标签: pandas boolean python-2.6 series missing-data


【解决方案1】:

你可以试试这个:

S[S[ind].index] = x

S[S.index[ind]] = x

【讨论】:

  • 感谢您的回答。我很好奇为什么会这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多