Python数据分析 知识量:13 - 56 - 232
可以对DataFrame对象的某列进行布尔计算,从而得到一个由布尔值构成的新列,就像下面这样:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.Chinese>60)
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 0 True 1 False 2 True 3 True 4 False 5 False 6 True 7 True Name: Chinese, dtype: bool
上面经过计算的Chinese列可以看做是一个布尔索引列,可以用该列值来过滤数据:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.Chinese>60) print(df[df.Chinese>60])
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 0 True 1 False 2 True 3 True 4 False 5 False 6 True 7 True Name: Chinese, dtype: bool Name Sex Chinese English Math 0 Noah male 90 50 66 2 Bob male 99 84 89 3 Olivia female 86 87 44 6 Sophia female 90 66 96 7 Isabella female 66 85 55
布尔索引值为True的行被选择出来了。
要查看数据表对象中是否包含某个值,需要使用isin()函数,其结果为一个布尔值。注意:isin()函数的参数必须是一个列表,用于表明要查找的值。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.Sex.isin(['male']))
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 0 True 1 False 2 True 3 False 4 True 5 True 6 False 7 False Name: Sex, dtype: bool
除了查看某一列,也可以对全表进行查找。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df.isin([55,66,90]))
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 Name Sex Chinese English Math 0 False False True False True 1 False False False False True 2 False False False False False 3 False False False False False 4 False False False False False 5 False False True False False 6 False False True True False 7 False False True False True
isin()函数的运行结果实际上就是一个布尔索引,因此,利用isin()函数可以查找满足条件的数据行。例如下面查找Chinese列含有数值90的行:
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df[df.Chinese.isin([90])])
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 Name Sex Chinese English Math 0 Noah male 90 50 66 6 Sophia female 90 66 96
还可以利用isin()函数对多个列进行个性化查找,并保留同时满足各列条件的行。
import pandas as pd df=pd.read_excel(r"D:\PythonTestFile\exam_new.xlsx") print(df,'\n') print(df[df.Chinese.isin([90]) & df.Sex.isin(['male'])]) # 查找Chinese为90且Sex为male的行
运行结果为:
Name Sex Chinese English Math 0 Noah male 90 50 66 1 Emma female 56 56 55 2 Bob male 99 84 89 3 Olivia female 86 87 44 4 Jeff male 48 87 65 5 Liam male 55 88 69 6 Sophia female 90 66 96 7 Isabella female 66 85 55 Name Sex Chinese English Math 0 Noah male 90 50 66
Copyright © 2017-Now pnotes.cn. All Rights Reserved.
编程学习笔记 保留所有权利
MARK:3.0.0.20240214.P35
From 2017.2.6