【发布时间】:2018-06-09 01:14:09
【问题描述】:
Pandas pd.Series.get 方法有一个默认参数(很像 dict.get),这表明我应该能够使用它在其索引中查找项目并返回行的值,但在查询时返回默认值不在索引中。
确实有效(相当于 .loc 其中 default=NAN):
# x10
maptable = pd.Series( [100, 110, 120, 130],
index=[10, 11, 12, 13]).sort_index()
query_vals = pd.Series([11,12,15], index=['A', 'B', 'C'])
# Passing list-likes to .loc or [] with any missing label will raise KeyError in the future, you can use .reindex() as an alternative.
print maptable.get(query_vals, float("nan"))
11 110.0
12 120.0
15 NaN
# Passing list-likes to .loc or [] with any missing label will raise KeyError in the future, you can use .reindex() as an alternative.
print maptable.loc[query_vals]
...但抱怨“将列表喜欢传递给 .loc 或 [] 任何缺少的标签将在未来引发 KeyError,您可以使用 .reindex() 作为替代方案。”
.get (over .loc) 的目的不正是为了允许查找缺失索引。
我应该怎么做才能避免这个警告?我不确定 .reindex 有什么帮助。
【问题讨论】: