Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 还使用NA值的Series.update()?_Python_Pandas - Fatal编程技术网

Python 还使用NA值的Series.update()?

Python 还使用NA值的Series.update()?,python,pandas,Python,Pandas,some_series.update(other_series)将覆盖some_series上的所有值,并使用other_series中的匹配值(按索引)覆盖这些值。但是,它确实会像“无”一样跳过所有NA值 如果我想从other_series更新某些_series,但也要应用NA值,我应该使用什么 澄清一下,这就是我想要的: In [197]: some_series Out[197]: 0 1 1 2 dtype: int64 In [198]: other_series Ou

some_series.update(other_series)
将覆盖
some_series
上的所有值,并使用
other_series
中的匹配值(按索引)覆盖这些值。但是,它确实会像“无”一样跳过所有NA值

如果我想从
other_series
更新
某些_series
,但也要应用NA值,我应该使用什么

澄清一下,这就是我想要的:

In [197]: some_series
Out[197]: 
0    1
1    2
dtype: int64

In [198]: other_series
Out[198]: 
0    None
dtype: object

In [203]: some_series  # after overriding it with other_series:
Out[203]: 
0    None
1       2
dtype: object

我不确定这是不是最好的方法,但这里有一种方法。您必须确保要更新的系列包括另一个系列(例如,使用reindex)

注意:没有这个阶段,如果你只是尝试更新,你会得到一个键错误


要仅更新s的索引,请使用交点:

In [21]: ind = t.index.intersection(s.index)

In [22]: s.loc[ind] = t.loc[ind]

In [23]: s
Out[23]:
0     1
1   NaN
dtype: float64

以下方法可行,它使用
loc
和其他系列索引来屏蔽我们要覆盖的元素:

In [106]:

some_series = pd.Series([1, 2])
other_series = pd.Series([None])
some_series.loc[other_series.index] = other_series
some_series
Out[106]:
0   NaN
1     2
dtype: float64

在这种情况下,
loc
是非常不必要的

这不是
some_series=other_series
所能做的吗?您使用update是因为这两个系列的索引范围不同吗?如果没有,我只会使用some_series=other_series.copy()@MarkGraph我认为这里没有必要显式调用
copy()
,我测试了它,它确实创建了对另一个的引用series@EdChum:似乎不是这样:
some_series=series([1,2]);其他_系列=系列([无]);一些系列=其他系列#=>(0无)
。我需要的是
(0 None,1 2)
。我不明白你怎么会期望这样的结果,因为看起来你想要的是类似于
pd.concat([其他系列,某些系列],忽略索引=True)
,而不是试图覆盖lhsThanks,但实际上我不希望
t
上任何不在
s
中的标签在
res
中。换句话说,我只想用相应的(按标签)
t
值覆盖
s
中的值。@DunPeal更新(使用交集而不是并集)谢谢!你所说的“loc在这种情况下是非常不必要的”是什么意思?我的意思是
some_series[other_series.index]=other_series
如果
other_series
包含
some_series
之外的索引,那么它也会起作用!这是一个关键错误(@AndyHayden是真的,但这个定义太模糊了,我已经放弃了进一步的润色,加上我正在观看《30rock》并决定如何浪费我的选票tomorrow@EdChum哈,你说这是一个定义不明确的-(I)!…不期待醒来的结果。手指交叉赞成。
some_series[other_series.index] = other_series
In [106]:

some_series = pd.Series([1, 2])
other_series = pd.Series([None])
some_series.loc[other_series.index] = other_series
some_series
Out[106]:
0   NaN
1     2
dtype: float64
some_series[other_series.index] = other_series