I want to know how to index the date with a specific value of Python data frame.

Asked 2 years ago, Updated 2 years ago, 59 views

Hello, I'm a beginner at ^. How do I reversely index an index or a date index with a specific value in a Python data frame?

^
df1 = pdDataFrame({'A': [1,4,7]},
                   index=[2019-04-07,2019-04-08,2019-04-09]

If there's a data frame like this, put in line 2 and 4 in column A We need a method to find the 2019-04-08 index value as a result.

Also, add the index value of column A line I would like to know how to find the 2019-04-08 date index value.

python dataframe

2022-09-21 19:07

1 Answers

It can be obtained using loc, iloc, as shown in the example below.

import pandas as pd

df1 = pd.DataFrame({'A': [1,4,7]}, index=['2019-04-07','2019-04-08','2019-04-09'])
df1.loc[df1.A == 4].index # df1.A[df1.A == 4].Same as index 
=> Index(['2019-04-08'], dtype='object')

df1.index[0] # First index
=> '2019-04-07'

df1.index[1] #Second index
=>'2019-04-08'

df1.A.iloc[[1]].index
=>Index(['2019-04-08'], dtype='object')


2022-09-21 19:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.