接种月龄计算
brk <- read_csv("/mnt/c/Users/xuefeng/Desktop/brk.csv",locale = locale(encoding = 'GB18030')) %>%
clean_names() %>%
mutate(jz_sj=ymd_hms(jz_sj),csrq=ymd(csrq),jzyl = interval(csrq, jz_sj) %/% months(1),shi=str_sub(gldw_bm,1,4))
brk %>%
filter(csrq <= ymd('2023-12-30') & jz_zc == 1) %>%
group_by(shi) %>%
summarise(count = n(),
jzyl_3 = sum(jzyl == 3,na.rm = T),jzyl_3/count*100) %>%
writexl::write_xlsx("/mnt/c/Users/xuefeng/Desktop/brk2024_3.xlsx")
import pandas as pd
import numpy as np
import janitor
from pandas.tseries.offsets import MonthEnd
from datetime import datetime
brk = pd.read_csv("/mnt/c/Users/xuefeng/Desktop/brk.csv", encoding='GB18030')
brk = (
brk
.rename(columns=lambda x: x.lower().replace(' ', '_'))
.assign(jz_sj=lambda df: pd.to_datetime(df['jz_sj'], format='%Y-%m-%d %H:%M:%S',errors='coerce'),
csrq=lambda df: pd.to_datetime(df['csrq'], format='%Y-%m-%d',errors='coerce'))
.assign(jzyl=lambda df: (df['jz_sj'] - df['csrq']) / pd.Timedelta(days=30.436875))
.assign(jzyl=lambda df: df['jzyl'].apply(np.floor))
.assign(shi=lambda df: df['gldw_bm'].astype(str).str.slice(0, 4))
)
(
brk
.query("csrq<='2023-12-31' & jz_zc==1")
.groupby('shi')
.agg(count=('shi', 'count'),
jzyl_3=('jzyl', lambda x: sum(x == 3)))
.reset_index()
.assign(prep=lambda df:df['jzyl_3']/df['count']*100)
)
评论
发表评论