Issue
I've looking around for this but I can't seem to find it (though it must be extremely trivial).
The problem that I have is that I would like to retrieve the value of a column for the first and last entries of a data frame. But if I do:
df.ix[0]['date']
I get:
datetime.datetime(2011, 1, 10, 16, 0)
but if I do:
df[-1:]['date']
I get:
myIndex
13 2011-12-20 16:00:00
Name: mydate
with a different format. Ideally, I would like to be able to access the value of the last index of the data frame, but I can't find how.
I even tried to create a column (IndexCopy) with the values of the index and try:
df.ix[df.tail(1)['IndexCopy']]['mydate']
but this also yields a different format (since df.tail(1)['IndexCopy'] does not output a simple integer).
Any ideas?
Solution
The former answer is now superseded by .iloc
:
>>> df = pd.DataFrame({"date": range(10, 64, 8)})
>>> df.index += 17
>>> df
date
17 10
18 18
19 26
20 34
21 42
22 50
23 58
>>> df["date"].iloc[0]
10
>>> df["date"].iloc[-1]
58
The shortest way I can think of uses .iget()
:
>>> df = pd.DataFrame({"date": range(10, 64, 8)})
>>> df.index += 17
>>> df
date
17 10
18 18
19 26
20 34
21 42
22 50
23 58
>>> df['date'].iget(0)
10
>>> df['date'].iget(-1)
58
Alternatively:
>>> df['date'][df.index[0]]
10
>>> df['date'][df.index[-1]]
58
There's also .first_valid_index()
and .last_valid_index()
, but depending on whether or not you want to rule out NaN
s they might not be what you want.
Remember that df.ix[0]
doesn't give you the first, but the one indexed by 0. For example, in the above case, df.ix[0]
would produce
>>> df.ix[0]
Traceback (most recent call last):
File "<ipython-input-489-494245247e87>", line 1, in <module>
df.ix[0]
[...]
KeyError: 0
Answered By - DSM
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.