Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Dict.get()' equivalent for pandas Series?

Does anyone know if there's an equivalent for the Dict.get() method for pandas Series? I've got a series that looks like this:

In [25]: s1
Out[25]: 
a    1
b    2
c    3
dtype: int64

And I'd like to return a default value, such as 0, if I try to access an index that isn't there. For example, I'd like s1.ix['z'] to return 0 instead of KeyError. I know pandas has great support for dealing with missing values in other circumstances, but I couldn't find anything specifically about this.

Thank you!

like image 747
kronosapiens Avatar asked Oct 01 '22 08:10

kronosapiens


1 Answers

As mentioned in the comments, pandas implements get directly for Series.

So s1.get('x', 0) would return 0

like image 158
kronosapiens Avatar answered Oct 03 '22 06:10

kronosapiens