Pandas8——excel圈释无效数据

excel效果展示

要求:

找出无效score所在行数据(有效score:0<=score<=100)

import pandas as pd

Stu=pd.read_excel("D:\\python_pandas\\sample\\demo10\\Students.xlsx")

def Validate(row):
    try:
        assert ((0<= row.Score) & (row.Score<= 100))
    except:
        print(f'#{row.ID}\tstudent {row.Name} has an invalid score {row.Score}')
Stu.apply(Validate,axis=1)

字符串前面加 f
    格式化字符串常量(formatted string literals),是Python3.6新引入的一种字符串格式化方法,主要目的是使格式化字符串的操作更加简便。f-string在形式上是以 f 或 F 修饰符引领的字符串(f'xxx' 或 F'xxx'),以大括号 {} 标明被替换的字段。

输出结果:
#1  student Student_001 has an invalid score -40
#2  student Student_002 has an invalid score -30
#3  student Student_003 has an invalid score -20
#4  student Student_004 has an invalid score -10
#16 student Student_016 has an invalid score 110
#17 student Student_017 has an invalid score 120
#18 student Student_018 has an invalid score 130
#19 student Student_019 has an invalid score 140
#20 student Student_020 has an invalid score 150

推荐视频链接

你可能感兴趣的:(Pandas8——excel圈释无效数据)