【问题标题】:How to get data 30 days data in Python如何在 Python 中获取数据 30 天数据
【发布时间】:2022-02-18 18:03:54
【问题描述】:

我正在尝试从表中获取 30 天的记录。

df['Form Modified Date']=pd.to_datetime(df['Form Modified Date'].astype(str), format='%Y/%m/%d')
#print (df)
timenow = str(datetime.now().strftime("%Y-%m-%d")) - 30 days

nya= df[(df['Form Modified Date'] == (timenow))]

【问题讨论】:

    标签: python pandas datetime timedelta


    【解决方案1】:

    你有几个选择:

    1. df.loc
    dayfrom = pd.to_datetime('1/23/2020')
    
    #set index from column Date
    df = df.set_index('Date')
    df= df.sort_index()
    
    #last 30 days of date lastday
    df = df.loc[dayfrom - pd.Timedelta(days=30):dayfrom].reset_index()
    
    1. DataFrame.last_valid_index()
    df[df.last_valid_index()-pandas.DateOffset(30, 'D'):]
    

    【讨论】:

      猜你喜欢
      • 2020-02-21
      • 1970-01-01
      • 2018-02-19
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      相关资源
      最近更新 更多