AnalysisNerd
AnalysisNerd

Reputation: 133

Selecting Rows with DateTimeIndex without referring to date

Is there a way to select rows with a DateTimeIndex without referring to the date as such e.g. selecting row index 2 (the usual Python default manner) rather than "1995-02-02"?

enter image description here

Thanks in advance.

Upvotes: 0

Views: 25

Answers (1)

dataista
dataista

Reputation: 3457

Yes, you can use .iloc, the positional indexer:

df.iloc[2]

Basically, it indexes by actual position starting from 0 to len(df), allowing slicing too:

df.iloc[2:5]

It also works for columns (by position, again):

df.iloc[:, 0]  # All rows, first column
df.iloc[0:2, 0:2] # First 2 rows, first 2 columns

Upvotes: 1

Related Questions