Home > Blockchain >  Have two dataframes with dt, How do I get the count of rows in the last 7 days?
Have two dataframes with dt, How do I get the count of rows in the last 7 days?

Time:04-24

import pandas as pd 



l1 = ["2021-11-15","2021-11-13","2021-11-10","2021-05-28","2021-06-02","2021-06-02","2021-11-02"]
l2 = ["2021-11-11","2021-03-02","2021-11-05","2021-05-20","2021-05-01","2021-06-01","2021-04-08"]
#convert to dt
l1=pd.to_datetime(l1)
l2= pd.to_datetime(l2)


#put in df
df1=pd.DataFrame(l1)
df2=pd.DataFrame(l2)
df1.columns = ['0']
df2.columns = ['0']
df1=df1.set_index('0')
df2=df2.set_index('0')

#sort asc
df1=df1.sort_index()
df2=df2.sort_index()

How can I get a COUNT from each dataframe based on the number of rows that are within the last 7 days?

CodePudding user response:

you can slice between two timestamps and then get the number of rows with .shape[0]:

def get_count_last_7_days(df):
    stop = df.index.max()
    start = stop - pd.Timedelta('7D')
    return df.loc[start:stop].shape[0]

count1 = get_count_last_7_days(df1)
count2 = get_count_last_7_days(df2)

CodePudding user response:

import pandas as pd
import numpy as np
from datetime import date, timedelta


x = (date.today() - timedelta(days=100))
y = (date.today() - timedelta(days=7))
z = date.today()

dates = pd.date_range(x, periods=100)
d = np.arange(1, 101)
df = pd.DataFrame(data=d, index=pd.DatetimeIndex(dates))


df = df.sort_index()

last_seven_days = df.loc[y:z]

print(last_seven_days.count())
  • Related