Reputation: 11049
I have a DataFrame with only 1 row in pandas.
The row has a index/name as a date.
I want to retrieve this date and the value in the 4th column.
I can get the value of the 4th column in this single row by using
first_row = data.iloc[0]
value = row[3]
but how can I get the corresponding date in this row?
I have tried first_row.index
but it wont work. If I use data.index
I get a DatetimeIndex with only one element but wouldn't it be better code if I could retrieve the index directly from the row?
Upvotes: 0
Views: 1033
Reputation: 394159
You have 2 choices, if you are performing integer indexing into the df, then using the same integer to index into the index will return that value.
If you have selected a specific row then you can access the index value by using the .name
attribute.
Example:
In [3]:
import io
import pandas as pd
t="""date,value
2014/01/05,1515
2014/02/05,15
2014/05/05,1"""
df=pd.read_csv(io.StringIO(t), parse_dates=[0], index_col=[0])
df
Out[3]:
value
date
2014-01-05 1515
2014-02-05 15
2014-05-05 1
In [7]:
df.index[1]
Out[7]:
Timestamp('2014-02-05 00:00:00')
In [16]:
df.iloc[1].name
Out[16]
Timestamp('2014-02-05 00:00:00')
Upvotes: 2