博文

polars管道

  def lowercase ( df : pl.DataFrame) -> pl.DataFrame:     """将 DataFrame 所有列名转换为小写"""     return df.rename({col: col.lower() for col in df.columns})     person = (         pl.read_csv(             "/mnt/c/Users/Administrator/Downloads/标准库接种率+v1.0.9-2024-12-27/标准库数据/person_standard.csv" ,         )         .pipe(lowercase)         .with_columns(             pl.col( "birth_weight" ).replace( "" , None )         )         .cast({             "id" : pl.String,             "birth_date" : pl.String,             "hepatitis_mothers" : pl.String,             "current_management_code" : pl.String,             "birth_weight" : pl.F...

免疫史计算

  vaccine_history = (     person.select( 'id_x' ).unique()     .join(         vaccine_tbl         .filter(pl.col( '大类编码' ).cast(pl.UInt32) < 20 )         .select( 'vaccine_name' )         .unique()         .filter(pl.col( 'vaccine_name' ).is_not_null()),         how = 'cross'     )     .join(         person         .group_by([ 'id_x' , 'vaccine_name' ])         .agg(pl.col( 'id_x' ).len().alias( '剂次数' )),         on = [ 'id_x' , 'vaccine_name' ],         how = 'left'     )     .with_columns(pl.col( '剂次数' ).fill_null( 0 ))     .sort([ 'id_x' , 'vaccine_name' ]) )

R 字段加密解密

  library ( tidyverse ) library ( janitor ) library ( hablar ) library ( openssl ) library ( magrittr ) library ( stringi ) # 加密函数 encrypt_column <- function ( data , column_names , key ) {   for ( column_name in column_names ) {     data [[ column_name ]] <- sapply ( data [[ column_name ]] , function ( x ) {       if ( is.na ( x ) || x == "" ) {         return ( NA )       }       tryCatch ({         x_utf8 <- stri_encode ( as.character ( x ) , "" , "UTF-8" )         iv <- rand_bytes ( 16 )         encrypted <- aes_cbc_encrypt ( charToRaw ( x_utf8 ) , key = key , iv = iv )         paste ( openssl :: base64_encode ( iv ) , openssl :: base64_encode ( encrypted ) , sep = ":" )       } , error = function ( e ) {         warning ( paste ( "Error ...

Polars 检查身份证号码是否符合规则

  import polars as pl def check_id ( id : str ) -> bool :     if len ( id ) != 18 :         return False         # 有效的地区码前两位(省份代码)     valid_provinces = {         '11' , '12' , '13' , '14' , '15' ,   # 北京、天津、河北、山西、内蒙古         '21' , '22' , '23' ,               # 辽宁、吉林、黑龙江         '31' , '32' , '33' , '34' , '35' , '36' , '37' ,   # 上海、江苏、浙江、安徽、福建、江西、山东         '41' , '42' , '43' , '44' , '45' , '46' ,         # 河南、湖北、湖南、广东、广西、海南         '50' , '51' , '52' , '53' , '54' ,               # 重庆、四川、贵州、云南、西藏         '61' , '62' , '63' , '64' , '65' ,               # 陕西、甘肃、青海、宁夏、新疆         '71' ,                ...

polars 长宽格式转换

  import polars as pl # 创建示例数据(宽格式) df_wide = pl.DataFrame({     "id" : [ 1 , 2 , 3 ],     "name" : [ "Alice" , "Bob" , "Charlie" ],     "math" : [ 90 , 85 , 92 ],     "english" : [ 88 , 90 , 85 ],     "science" : [ 92 , 88 , 90 ] }) print ( "原始数据(宽格式):" ) print (df_wide) # 使用 melt() 转换为长格式 df_long = df_wide.unpivot(     index = [ "id" , "name" ],           # 保持不变的列     on = [ "math" , "english" , "science" ],   # 要转换的列     variable_name = "subject" ,           # 新的变量名列     value_name = "score"               # 新的值列 ) # 使用上面的长格式数据,转换回宽格式 df_back_to_wide = df_long.pivot(     values = "score" ,       # 用作值的列     index = [ "id" , "name" ], # 作为行索引的列     on = "subject"     # 作为列名的列 ) print ( "转换回宽格式:" ) print (df_back_to_wide)

polars

import polars as pl df = pl.read_csv(     "/mnt/c/Users/Administrator/Downloads/百日咳小于2月大于7岁接种1.csv" ,     schema_overrides = {         "编码" : pl.Utf8,         "出生日期" : pl.Date,         "百日咳接种日期" : pl.Date,     },   # 将编码列指定为字符串类型 ) df.filter(pl.col( "出生日期" ) > pl.date( 2025 , 1 , 25 )) df.glimpse() df.head() df.group_by(pl.col( "出生日期" )).agg(pl.col( "编码" ).n_unique()).sort( "出生日期" ) df.group_by(pl.col( "出生日期" )).agg(pl.col( "编码" ).count()).sort( "出生日期" ) df = pl.DataFrame(     {         "name" : [ "Alice" , "Bob" , "Charlie" , "David" ],         "department" : [ "HR" , "IT" , "Finance" , "IT" ],         "salary" : [ 70000 , 80000 , 120000 , 95000 ],     } ) df = df.with_columns(     [pl.col( "salary" ).cum_sum().over( "department" ).alias( ...

R安装包时强制使用系统环境

  # 临时退出 conda conda deactivate # 安装系统版本 sudo apt update sudo apt install r-base-dev libnlopt-dev # 在系统 R 中安装 R -e "install.packages('nloptr', repos='https://cloud.r-project.org/')" R -e "install.packages('lme4', repos='https://cloud.r-project.org/')"