Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get() for default values in Pandas Series, using position

Tags:

python

pandas

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)?

like image 617
vk1011 Avatar asked Jul 08 '16 08:07

vk1011


People also ask

How do I get Pandas series values?

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.

How do you reference a value in a Pandas series?

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.

Does ILOC work on series?

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.

How do I change the values in Pandas series based on conditions?

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.


1 Answers

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')
like image 75
Gerges Avatar answered Oct 25 '22 01:10

Gerges