数据分析-day03-pandas-dataFrame的bool 索引使用
print(df["Count_AnimalName"]>10) #打印出都是true,false
print(df[df["Count_AnimalName"]>10]) #获取数据
注意这两个不同写法,代表不同功能
# @File : pandas_dataframe_boolindex_demo.py
# @Date : 2020-01-02 20:17
# @Author : admin
import string
import pandas as pd;
import numpy as np;
df=pd.read_csv("../../data/dogNames2.csv");
print(df)
print(type(df))
print("=======获取列")
print(df["Count_AnimalName"])
print("使用bool索引.设置判断条件")
print(df["Count_AnimalName"]>10) #打印出都是true,false
print(">>>>>>>使用bool索引获取数据<<<<<<<")
print(df[df["Count_AnimalName"]>10])
print("使用bool索引.设置与或非条件")
# 与&,或|
print(df[(df["Count_AnimalName"]>10)&(df["Row_Labels"]=='SUNDAY')])
#结果:
Row_Labels Count_AnimalName
0 1 1
1 2 2
2 40804 1
3 90201 1
4 90203 1
... ... ...
16215 37916 1
16216 38282 1
16217 38583 1
16218 38948 1
16219 39743 1
[16220 rows x 2 columns]
<class 'pandas.core.frame.DataFrame'>
=======获取列
0 1
1 2
2 1
3 1
4 1
..
16215 1
16216 1
16217 1
16218 1
16219 1
Name: Count_AnimalName, Length: 16220, dtype: int64
使用bool索引.设置判断条件
0 False
1 False
2 False
3 False
4 False
...
16215 False
16216 False
16217 False
16218 False
16219 False
Name: Count_AnimalName, Length: 16220, dtype: bool
>>>>>>>使用bool索引获取数据<<<<<<<
Row_Labels Count_AnimalName
8 APRIL 51
9 AUGUST 14
11 SUNDAY 13
13 FRIDAY 19
17 JUNE 24
... ... ...
16157 ZORA 13
16164 ZORRO 35
16175 ZSA 12
16195 ZULU 11
16208 ZUZU 16
[1468 rows x 2 columns]
使用bool索引.设置与或非条件
Row_Labels Count_AnimalName
11 SUNDAY 13