Reputation: 636
I define a Pandas DataFrame as a = pd.DataFrame({'val':[1,2,3,4,5,6],'id':[2,1,4,3,0,5]})
, such that:
In [0]: import pandas as pd
In [1]: a = pd.DataFrame({'val':[1,2,3,4,5,6],'id':[2,1,4,3,0,5]})
In [2]: a
Out[2]:
id val
0 2 1
1 1 2
2 4 3
3 3 4
4 0 5
5 5 6
Also, I perform some functions over it, like a.sort('id',inplace=True)
, such that:
In [3]: a
Out[3]:
id val
4 0 5
1 1 2
0 2 1
3 3 4
2 4 3
5 5 6
Note how the index is unordered.
And after, I want to extract information by doing something like a.val[a.id >=2]
, which returns a pd.Series
:
In [4]: a.val[a.id >=2]
Out[4]:
0 1
3 4
2 3
5 6
Name: val, dtype: int64
If I want to extract the first value of the pd.Series
I do a.val[a.id >= 2].iloc[0]
, and for the index a.val[id >= 2].index[0]
, and put them as a tuple.
Is there a way to extract both values as a tuple in one line of code? Is there a pythonic way?
Upvotes: 1
Views: 830
Reputation: 309929
Disclaimer: I'm not a real pandas user (though I like to read pandas questions around here), so there could be a better way...
That said, if pandas doesn't provide a better way, I'd just write a function:
def extract_first(series):
return series.iloc[0], series.index[0]
extract_first(a.val[a.id >= 2])
I would expect that this would allow you to execute the comparatively expensive indexing only once which could be helpful for larger series.
Looking at the docs, it appears that you can also use iteritems
:
next(a.val[a.id >= 2].iteritems())
Upvotes: 2