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!
As mentioned in the comments, pandas implements get
directly for Series.
So s1.get('x', 0)
would return 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With