【问题标题】:How to get index number of rows with condition in pandas如何在熊猫中获取有条件的行的索引数
【发布时间】:2021-09-15 08:34:24
【问题描述】:

我有一个数据框,我需要在其中与日期列进行比较,并需要获取索引号。

条件:-

  1. 如果日期 == 01-06-2018 则获取包含该日期的所有行的索引号。

  2. 否则,将其与 01-06-2018 之前的确切日期进行比较,并且该日期在此数据框中为 01-12-2017。

那么现在我们如何使用第二个条件获取包含 01-12-2017 日期的所有行的索引号。

数据框:-

 SR.No     Date
  1      01-12-2013
  1      01-12-2014
  1      01-12-2015
  1      01-12-2016
  1      01-12-2017 
  1      01-12-2018
  1      01-12-2019
  1      01-12-2020 
  1      01-12-2013
  1      01-12-2014
  1      01-12-2015
  1      01-12-2016
  1      01-12-2017 
  1      01-12-2018
  1      01-12-2019
  1      01-12-2020 
  2      01-12-2013
  2      01-12-2014
  2      01-12-2015
  2      01-12-2016
  2      01-12-2017
  2      01-12-2018
  2      01-12-2019 
  2      01-12-2020 
  2      01-12-2013
  2      01-12-2014
  2      01-12-2015
  2      01-12-2016
  2      01-12-2017
  2      01-12-2018
  2      01-12-2019 
  2      01-12-2020

【问题讨论】:

  • 顺便说一句,您的日期格式是什么?dd-mm-yymm-dd-yy
  • 日期格式为 dd-mm-yy。
  • gotcha....更新的答案请看一下:)

标签: python pandas date indexing


【解决方案1】:

您可以创建自定义函数:

def getdate(date,df=df):
    df=df.copy()
    df['Date']=pd.to_datetime(df['Date'],dayfirst=True)
    date=pd.to_datetime(date,dayfirst=True)
    if df['Date'].eq(date).any():
        return df[df['Date'].eq(date)].index
    else:
        date=(date-pd.DateOffset(months=6))
        return df[df['Date'].eq(date)].index

最后:

out=getdate('01-06-2018')

out的输出:

Int64Index([4, 12, 20, 28], dtype='int64')

说明:

我们正在创建一个函数,它检查给定日期(当前给出 '01-06-2018')是否等于 df['Date'] 系列中的日期,它返回布尔系列,我们将该布尔系列传递给 DataFrame @ 987654326@ 并使用.index 属性过滤结果并获取索引,这发生在if 块和else 块中,如果数据框中不存在我们在if 块中检查的条件的行然后我们减去,或者您也可以说将日期四舍五入到前 6 个月(使用pd.DateOffset())所以现在日期(您输入的日期(当前为 '01-06-2018')变为 '01-12-2017 '(因为我们在函数内部使用了to_datetime() 方法,所以它变成了'2017-12-01')所以我们正在检查这个日期是否存在于你的数据框的'df3'列中,如果它存在那么它将给你索引,否则它会给你空系列

函数内每一行的解释:

def getdate(date,df=df):
    df=df.copy()
    #creating the copy of the dataframe so that the changes made here doesn't reflect in your original dataframe
    df['Date']=pd.to_datetime(df['Date'],dayfirst=True)
    #converting the Date column to datetime dtype
    date=pd.to_datetime(date,dayfirst=True)
    #converting the date that you passed when calling the function '01-06-2018' to datetime
    if df['Date'].eq(date).any():
    #checking if '01-06-2018' is present in 'Date' column
        return df[df['Date'].eq(date)].index
        #Filtering out result and getting index
    else:
    #If '01-06-2018' is not present in 'Date' column then
        date=(date-pd.DateOffset(months=6))
        #Rounding/substracting the 6 months so now date become '2017-12-01'
        return df[df['Date'].eq(date)].index
        #Filtering out result and getting index

更新:

添加条件后你的函数变成:

def getdate(date,df=df):
    df=df.copy()
    df['Date']=pd.to_datetime(df['Date'],dayfirst=True)
    date=pd.to_datetime(date,dayfirst=True)
    if df['Date'].eq(date).any():
        return df[df['Date'].eq(date)].index
    else:
        to_check=pd.to_datetime(pd.Series(['01-03-18','01-06-2018','01-09-2018']),dayfirst=True)
        if to_check.isin([date]).any():
            date=pd.to_datetime('01-12-2017',dayfirst=True)
        else:
            date=(date-pd.DateOffset(months=6))
        return df[df['Date'].eq(date)].index

【讨论】:

  • 日期格式为 dd-mm-yyyy。你试过这种格式吗?你能解释一下这个其他部分吗?其实这个功能对我来说是新的。谢谢!!
  • @Yaser 更新了答案....请看一下:)
  • 谢谢,这是一个很好的解释 :) 但如果我们有 01-03-2018 或 01-09-2018 而不是 01-06-2018 那么这些日期也适用吗?跨度>
  • @Yaser 是的,如果提供的日期存在于数据框中,它将适用于任何具有相同条件的日期,那么它将给出索引,如果它不存在,则它将日期舍入到 6前一个月并检查该日期,如果它也无法找到该日期,那么它将返回空系列
  • @Yaser 更新了答案...请看一下:)
猜你喜欢
  • 2022-08-19
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 2018-05-12
  • 2017-12-23
  • 2022-07-25
相关资源
最近更新 更多