sam
sam

Reputation: 19164

Dataframe get index date from value

I have a df

enter image description here

and in iteration with print statement,

print(df.iloc[i])

I am getting value as

enter image description here

But how to get the index value of it which is a date from df.iloc[i]?

Upvotes: 0

Views: 1052

Answers (2)

user2415706
user2415706

Reputation: 972

We have to reset the index since can't perform transformations on the index afaik.

New date column looks like datetime object so we have to import it.

The code below would create a new column with only the year-month-day in a new column. You can always drop the old date column later.

import datetime as dt
df.reset_index()
df['Y-M-D-Only'] = df['Date'].dt.date
for i in df['Y-M-D-Only']:
    print(i)

Upvotes: 0

Lucas Ford
Lucas Ford

Reputation: 181

df.iloc[i].name should give you the index for that particular row.

Upvotes: 3

Related Questions