如果 pandas dataframe.loc 位置不存在,则返回默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23403352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:59:30  来源:igfitidea点击:

return default if pandas dataframe.loc location doesn't exist

pythonpandas

提问by fantabolous

I find myself often having to check whether a column or row exists in a dataframe before trying to reference it. For example I end up adding a lot of code like:

我发现自己在尝试引用之前经常必须检查数据框中是否存在列或行。例如,我最终添加了很多代码,例如:

if 'mycol' in df.columns and 'myindex' in df.index: x = df.loc[myindex, mycol]
else: x = mydefault

Is there any way to do this more nicely? For example on an arbitrary object I can do x = getattr(anobject, 'id', default)- is there anything similar to this in pandas? Really any way to achieve what I'm doing more gracefully?

有没有办法更好地做到这一点?例如,在我可以做的任意对象上x = getattr(anobject, 'id', default)- 在Pandas中是否有类似的东西?真的有什么方法可以更优雅地实现我正在做的事情吗?

回答by EdChum

There is a method for Series:

有一种方法Series

So you could do:

所以你可以这样做:

df.mycol.get(myIndex, NaN)

Example:

例子:

In [117]:

df = pd.DataFrame({'mycol':arange(5), 'dummy':arange(5)})
df
Out[117]:
   dummy  mycol
0      0      0
1      1      1
2      2      2
3      3      3
4      4      4

[5 rows x 2 columns]
In [118]:

print(df.mycol.get(2, NaN))
print(df.mycol.get(5, NaN))
2
nan

回答by FooBar

Python has this mentality to ask for forgiveness instead of permission. You'll find a lot of posts on this matter, such as this one.

Python 有这种请求宽恕而不是许可的心态。你会发现很多关于这个问题的帖子,比如这个

In Python catching exceptions is relatively inexpensive, so you're encouraged to use it. This is called the EAFPapproach.

在 Python 中捕获异常的成本相对较低,因此鼓励您使用它。这称为EAFP方法。

For example:

例如:

try:
    x = df.loc['myindex', 'mycol']
except KeyError:
    x = mydefault