博文

目前显示的是 九月, 2024的博文

ibis 测试

import ibis import pandas as pd from ibis import _ penguins = ibis . examples .penguins. fetch () df = pd . DataFrame (     [[ "a" , 1 , 2 ], [ "b" , 3 , 4 ]],     columns = [ "one" , "two" , "three" ],     index = [ 5 , 6 ], ) t = ibis . memtable ( df , name = "t" ) ( penguins . filter ([ _ .species == "Adelie" , _ .island.isin([ "Biscoe" , "Dream" ])])) ( penguins . limit ( 11 , offset = 9 )) ( penguins . order_by ([ _ .island, _ .bill_length_mm.desc()])) ( penguins . select ( _ .bill_length_mm, _ .sex)) ( penguins . rename ( bill_length = "bill_length_mm" , bill_depth = "bill_depth_mm" )) (     penguins . mutate ( ones = 1 , bill_length = _ .bill_length_mm / 10 )     . mutate ( bill_length_squared = _ .bill_length ** 2 )     . select ( _ .ones, _ .bill_length_mm, _ .bill_length, _ .bill_length_squared) ) (     penguins . select (         _ .island,         _ .

pandas case_when 使用

 jz= pd.read_sql(sqltxt, conn).rename(columns=str.lower) jz=(     jz     .assign(         age=lambda x: (x['jz_sj']-x['csrq']).dt.days//365,         age_group=lambda df:df.age.case_when(             [                 (lambda s:(s>=15) & (s<=44),'15-44'),                 (lambda s:(s>=45) & (s<=59),'45-59'),                 (lambda s:s>=60,'>60'),                 (lambda s:s<15,'<15'),             ]         ),         year=lambda x: x['jz_sj'].dt.strftime('%Y')     ) ) (     jz.query("age_group!='unknown' & jzdd_dm.str.len()==10")     .groupby(['jzdd_mc','jzdd_dm','year','age_group'],as_index=False).agg(n=('ym_mc',np.size))     .pivot_table(index=['jzdd_mc','jzdd_dm'],columns=['year','age_group'],values='n')     .sort_values('jzdd_dm') )