Is there a way in Pandas to get a default value, when accessing a row by position? I am aware of the .get()
function, but that works when searching by index.
Below is what I want to do. The DataFrame:
In [24]: df
Out[24]:
col1
idx
20 A
21 B
22 C
23 D
24 E
Searching by index and getting a default value works fine:
In [25]: df['col1'].get(23, 'the_default_value')
Out[25]: 'D'
In [26]: df['col1'].get(28, 'the_default_value')
Out[26]: 'the_default_value'
But there doesn't appear to be an equivalent way to search by position. I can use .iloc()
, but it doesn't help in getting a default value if that row is not present. Eg.
In [57]: df['col1'].iloc[2]
Out[57]: 'C'
In [58]: df['col1'].iloc[6]
...
IndexError: single positional indexer is out-of-bounds
I can set it up using try...except
, or check beforehand if the value exists or not, but is there a cleaner way of doing it, like .iget()
(like the .loc
vs .iloc
)?
get() function to get the value for the passed index label in the given series object. Output : Now we will use Series. get() function to return the value for the passed index label in the given series object.
To reference an element of a pandas series object, all you have to do is called the name of the pandas series object followed by the index, or label, in brackets.
iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
Would something like this be considered cleaner:
df['new_index'] = np.arange(df.shape[0])
df = df.set_index('new_index')
df['col1'].get(2, 'the_default_value')
If the original index is required, then, it may be useful to use multi-index
df['new_index'] = np.arange(df.shape[0])
df = df.set_index('new_index', append=True)
df['col1'].get((pd.IndexSlice[:], 2), 'the_default_value')
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