How to use test_rename_columns method in pandera

Best Python code snippet using pandera_python

testpandaszyz.py

Source: testpandaszyz.py Github

copy

Full Screen

...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:...

Full Screen

Full Screen

testrenamefunc.py

Source: testrenamefunc.py Github

copy

Full Screen

...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)...

Full Screen

Full Screen

test_utils.py

Source: test_utils.py Github

copy

Full Screen

...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

Full Screen

Full Screen

Blogs

Check out the latest blogs from LambdaTest on this topic:

Test strategy and how to communicate it

I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.

How To Write End-To-End Tests Using Cypress App Actions

When I started writing tests with Cypress, I was always going to use the user interface to interact and change the application’s state when running tests.

Pair testing strategy in an Agile environment

Pair testing can help you complete your testing tasks faster and with higher quality. But who can do pair testing, and when should it be done? And what form of pair testing is best for your circumstance? Check out this blog for more information on how to conduct pair testing to optimize its benefits.

LIVE With Automation Testing For OTT Streaming Devices ????

People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.

Different Ways To Style CSS Box Shadow Effects

Have you ever visited a website that only has plain text and images? Most probably, no. It’s because such websites do not exist now. But there was a time when websites only had plain text and images with almost no styling. For the longest time, websites did not focus on user experience. For instance, this is how eBay’s homepage looked in 1999.

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful