Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How do I get the key (index) of the first and last row of a dataframe

I have a dataframe (df) with a datetime index and one field "myfield"

I want to find out the first and last datetime of the dataframe.

I can access the first and last dataframe element like this:

df.iloc[0]
df.iloc[-1]

for df.iloc[0] I get the result:

myfield myfieldcontent

Name: 2017-07-24 00:00:00, dtype: float

How can I access the datetime of the row?

like image 217
576i Avatar asked Jul 26 '17 07:07

576i


People also ask

How do I print the first and last row in pandas?

pandas. Series is easier to get the value. You can get the first row with iloc[0] and the last row with iloc[-1] . If you want to get the value of the element, you can do with iloc[0]['column_name'] , iloc[-1]['column_name'] .

How do I get pandas row index?

There may be many times when you want to be able to know the row number of a particular value, and thankfully Pandas makes this quite easy, using the . index() function. Practically speaking, this returns the index positions of the rows, rather than a row number as you may be familiar with in Excel.

How do you index the first row in a Dataframe?

Method 1: Using iloc[] This method is used to access the row by using row numbers. We can get the first row by using 0 indexes. we can also provide the range index. Here the rows will be extracted from the start till the index -1 mentioned after the right side of :.


3 Answers

You can use select index by [0] or [-1]:

df = pd.DataFrame({'myfield':[1,4,5]}, index=pd.date_range('2015-01-01', periods=3))
print (df)
            myfield
2015-01-01        1
2015-01-02        4
2015-01-03        5

print (df.iloc[-1])
myfield    5
Name: 2015-01-03 00:00:00, dtype: int64

print (df.index[0])
2015-01-01 00:00:00

print (df.index[-1])
2015-01-03 00:00:00
like image 156
jezrael Avatar answered Oct 06 '22 13:10

jezrael


If you are using pandas 1.1.4 or higher, you can use "name" attribute.

import pandas as pd

df = pd.DataFrame({'myfield': [1, 4, 5]}, index=pd.date_range('2015-01-01', periods=3))
df = df.reset_index()
print("Index value: ", df.iloc[-1].name) #pandas-series
#Convert to python datetime
print("Index datetime: ", df.iloc[-1].name.to_pydatetime()) 
like image 2
Sekhar Divakaruni Avatar answered Oct 06 '22 13:10

Sekhar Divakaruni


jezrael's answer is perfect. Just to provide an alternative, if you insist on using loc then you should first reset_index.

import pandas as pd

df = pd.DataFrame({'myfield': [1, 4, 5]}, index=pd.date_range('2015-01-01', periods=3))
df = df.reset_index()
print df['index'].iloc[0]
print df['index'].iloc[-1]
like image 1
Chankey Pathak Avatar answered Oct 06 '22 12:10

Chankey Pathak