Ashat Usbekof
Ashat Usbekof

Reputation: 35

How to get data 30 days data in Python

I'm trying to get 30 days records from the table.

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))]

Upvotes: 0

Views: 367

Answers (1)

Tal Folkman
Tal Folkman

Reputation: 2571

you have a few options:

  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'):]

Upvotes: 1

Related Questions