1

I have the data of pandas dataframe.

for i,row in stock.iterrows():
  pprint(row)

it returns data like this

Data             3.25000
Name: 2000-01-03 00:00:00, dtype: float64

I can access 3.25000 as row['Data'], however row['Name'] is unavailable.

How can I access 2000-01-03 00:00:00 ???

I have used dataframe with index.

Name means the same as index???

1 Answer 1

2

I think Name is index name.

You need i for index values:

for i,row in stock.iterrows():
    pprint(row)
    #index
    pprint(i)

Sample:

stock = pd.DataFrame({'Data':[3.5, 5.6]}, index=pd.date_range('2000-01-03', periods=2))
stock.index.name = 'Name'
print (stock)
            Data
Name            
2000-01-03   3.5
2000-01-04   5.6

print (stock.index.name)
Name

print (stock.index)
DatetimeIndex(['2000-01-03', '2000-01-04'], dtype='datetime64[ns]', name='Name', freq='D')

for i,row in stock.iterrows():
    pprint(row)
    #index
    pprint(i)

Data    3.5
Name: 2000-01-03 00:00:00, dtype: float64
Timestamp('2000-01-03 00:00:00', freq='D')
Data    5.6
Name: 2000-01-04 00:00:00, dtype: float64
Timestamp('2000-01-04 00:00:00', freq='D')

index name is converted to column name after reset_index:

stock = stock.reset_index()
print (stock)
        Name  Data
0 2000-01-03   3.5
1 2000-01-04   5.6

print (stock.index.name)
None

print (stock.index)
RangeIndex(start=0, stop=2, step=1)

print (stock.columns)
Index(['Name', 'Data'], dtype='object')
Sign up to request clarification or add additional context in comments.

2 Comments

THanks very much, I need to read the reference of pandas again. And your example is quite helpful!
@whitebear - Glad can help. Nice day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.