python 3.x - Pandas: How do I get the key (index) of the first and last row of a dataframe -
i have dataframe (df) datetime index , 1 field "myfield"
i want find out first , last datetime of dataframe.
i can access first , last dataframe element this:
df.iloc[0] df.iloc[-1]
for df.iloc[0]
result:
myfield myfieldcontent
name: 2017-07-24 00:00:00, dtype: float
how can access datetime of row?
you can use select index
[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
Comments
Post a Comment