Best Python code snippet using pandera_python
testpandaszyz.py
Source:testpandaszyz.py
...54 df_new = df["Surname_Age"].str.split("_", expand =True)55 print(df_new)56#8. å段çå½å57#æ两ç§æ¹å¼ä¸ç§æ¯ä½¿ç¨rename()å½æ°, å¦ä¸ç§æ¯ç´æ¥è®¾ç½®columnsåæ°58def test_rename_columns():59 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28" ]})60 print(df)61 print("第ä¸ç§æ¹æ³ä½¿ç¨rename()å½æ°")62 df_new = df["Surname_Age"].str.split("_", expand =True).rename(columns={0: "Surname", 1: "Age"})63 print(df_new)64 print("第äºç§æ¹æ³ç´æ¥è®¾ç½®columnsåæ°")65 df_new = df["Surname_Age"].str.split("_", expand =True)66 df_new.columns = ["Surname","Age"]67 print(df_new)68#9. å段çå并69#使ç¨merge()å½æ°å¯¹å段è¿è¡å并æä½.70def test_merge():71 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28" ]})72 print(df)73 df_new = df["Surname_Age"].str.split("_", expand =True)74 df_new.columns = ["Surname","Age"]75 print(df_new)76 print("使ç¨mergeå½æ°å¯¹ä¸¤è¡¨çå段è¿è¡å并æä½.")77 print(pd.merge(df, df_new, left_index =True, right_index=True))78#10. å段çå é¤79#å©ç¨drop()å½æ°å¯¹å段è¿è¡å é¤.80def test_drop():81 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28" ]})82 print(df)83 df_new = df["Surname_Age"].str.split("_", expand =True)84 df_new.columns = ["Surname","Age"]85 print(df_new)86 df_mer= pd.merge(df, df_new, left_index =True, right_index=True)87 print(df_mer)88 print("drop()å é¤å段,第ä¸ä¸ªåæ°æè¦å é¤çå段,axis=1表示å段æå¨å,inplace为True表示å¨å½å表æ§è¡å é¤.")89 df_mer.drop("Surname_Age", axis = 1, inplace =True)90 print(df_mer)91#11. è®°å½çæ½å92def test_chouqu():93 print("1) å
³ç³»è¿ç®: df[df.å段å å
³ç³»è¿ç®ç¬¦ æ°å¼], æ¯å¦æ½åå¹´é¾å¤§äº30å²çè®°å½.")94 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28" ]})95 print(df)96 df_new = df["Surname_Age"].str.split("_", expand =True)97 print(df_new)98 df_new.columns = ["Surname","Age"]99 print(df_new)100 df_mer= pd.merge(df, df_new, left_index =True, right_index=True)101 print(df_mer)102 df_mer.drop("Surname_Age", axis = 1, inplace =True)103 print(df_mer)104 print("å°Ageå段æ°æ®ç±»å转å为æ´å")105 df_mer["Age"] = df_mer["Age"].astype(int)106 print(df_mer)107 print("æ½åAgeä¸å¤§äº30çè®°å½")108 df_mer[df_mer.Age > 30]109 print(df_mer)110 print("2) èå´è¿ç®: df[df.å段å.between(s1, s2)], 注ææ¢å
å«s1åå
å«s2, æ¯å¦æ½åå¹´é¾å¤§äºçäº23å°äºçäº28çè®°å½. ")111 df_mer[df_mer.Age.between(23,28)]112 print(df_mer)113 print("3) é»è¾è¿ç®: ä¸(&) æ(|) é(not)")114 print("æ¯å¦ä¸é¢çèå´è¿ç®df_mer[df_mer.Age.between(23,28)]å°±çåäºdf_mer[(df_mer.Age >= 23) & (df_mer.Age <= 28)]")115 df_mer[(df_mer.Age >= 23 ) & (df_mer.Age <= 28)]116 print(df_mer)117 print("4) å符å¹é
: df[df.å段å.str.contains(\"å符\", case = True, na =False)] contains()å½æ°ä¸case=True表示åºå大å°å, é»è®¤ä¸ºTrue; na = False表示ä¸å¹é
缺失å¼.")118 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28"],"SpouseAge":[np.NaN,"32",np.NaN]})119 print(df)120 print("å¹é
SpouseAgeä¸å
å«2çè®°å½")121 df[df.SpouseAge.str.contains("2",na = False)]122 print(df)123 print("5) 缺失å¼å¹é
:df[pd.isnull(df.å段å)]表示å¹é
该å段ä¸æ缺失å¼çè®°å½.")124 df = pd.DataFrame({"ID": [100000,100101,100201],"Surname_Age": ["Zhao_23","Qian_33","Sun_28"],"SpouseAge":[np.NaN,"32",np.NaN]})125 print(df)126 print("å¹é
SpouseAgeä¸æ缺失å¼çè®°å½")127 df[pd.isnull(df.SpouseAge)]128 print(df)129#12.è®°å½çå并130def test_concat():131 #使ç¨concat()å½æ°å¯ä»¥å°ä¸¤ä¸ªæè
å¤ä¸ªæ°æ®è¡¨çè®°å½å并ä¸èµ·, ç¨æ³: pandas.concat([df1, df2, df3.....])132 df1 = pd.DataFrame({"ID": ["A10006","A10001"],"Salary": [12000, 20000]})133 df2 = pd.DataFrame({"ID": ["A10008"], "Salary": [10000]})134 print("使ç¨concat()å½æ°å°df1ä¸df2çè®°å½è¿è¡å并")135 print(pd.concat([df1, df2]))136#13. ç®å计ç®æ°å»ºä¸ä¸ªæ°æ®è¡¨df137def test_newdataframe():138 df = pd.DataFrame({"å°åº": ["Aåº","Båº","Cåº"],139 "ååå¹´éé": [3500, 4500,3800],140 "ååå¹´éé": [3000, 6000,5000],141 "åä»·": [10, 18, 15]})142 print(df)143test_drop_duplicates()144test_fillna()145test_dropna()146test_dtypes()147test_astype()148test_slice()149test_split()150test_rename_columns()151test_merge()152test_drop()153test_chouqu()154test_concat()155test_newdataframe()156#xuanxiang={"1":"éå¤å¼çå¤ç","2":"è¡¥é½ç¼ºå¤±å¼","3":"å é¤ç¼ºå¤±å¼","4":"æ¥çæ°æ®ç±»å","5":"ä¿®æ¹æ°æ®ç±»å","6":"å段çæ½å",157 #"7":"å段çæå","8":"å段çå½å","9":"å段çå并","10":"å段çå é¤","11":"è®°å½çæ½å","12":"è®°å½çå并","13":"ç®åå建æ°æ®è¡¨"}158#print(xuanxiang)159#c=input("è¾å
¥ç¬¬å 个æµè¯\n")160#print(c)161#if c=='1':162# test_drop_duplicates()163#elif c=='2':164# test_fillna()165#elif c=='3':166# test_dropna()167#elif c=='4':168# test_dtypes()169#elif c=='5':170# test_astype()171#elif c=='6':172# test_slice()173#elif c=='7':174# test_split()175#elif c=='8':176# test_rename_columns()177#elif c=='9':178# test_merge()179#elif c=='10':180# test_drop()181#elif c=='11':182# test_chouqu()183#elif c=='12':184# test_concat()185#elif c=='13':186# test_newdataframe()187#else:...
testrenamefunc.py
Source:testrenamefunc.py
...16 :return: sparkdataframe17 """18 df_renamed = df.select([F.col(c).alias(col_rename.get(c, c)) for c in df.columns])19 return df_renamed20def test_rename_columns():21 df = spark.read.format("csv").option("header", "true").option('delimiter', ',').load("data/Input/dataset_one.csv")22 col_rename = {"id": "client_identifierx"}23 source_df = rename_columns(df, col_rename)24 print("ashok:" + str(source_df.columns))25 # Create Schema26 schema = StructType([27 StructField('client_identifier', StringType(), True),28 StructField('first_name', StringType(), True),29 StructField('last_name', StringType(), True),30 StructField('email', StringType(), True),31 StructField('country', StringType(), True)32 ])33 expected_df = spark.read.csv(path="data/Input/dataset_one.csv", schema=schema, header=True)34 expected_df.printSchema()35 assert_df_equality(source_df, expected_df)...
test_utils.py
Source:test_utils.py
...14tbl = QTable([[2.0, 5.0], ["x", "y"]], names=("a", "b"))15##############################################################################16# CODE17##############################################################################18def test_rename_columns():19 """Test `~astronat.utils.table.utils.rename_columns`."""20 utils.rename_columns(table=tbl, rename={"a": "A"})21 assert tbl.colnames == ["A", "b"]22 utils.rename_columns(table=tbl, rename={"A": "a"})23 assert tbl.colnames == ["a", "b"]24# /def25# -------------------------------------------------------------------26def test_cast_columns():27 """Test `~astronat.utils.table.utils.rename_columns`."""28 utils.cast_columns(table=tbl, recast={"a": lambda x: x * u.km})29 assert all(tbl["a"] == [2.0, 5.0] * u.km)30# /def31##############################################################################32# END
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!