第九章:Pandas模块的介绍-数据子集的筛选

直接学习:https://edu.csdn.net/course/play/6861/326767
数据子集的筛选
1、语法
列的筛选:df.column-name,df[‘column-name’]
行的筛选:df.loc[condition,:]
行列的筛选:df.loc[condition,:column_list]

import pandas as pd
import pymysql
bridge = pymysql.connect(host = '127.0.0.1',user = 'root', password = '1q2w3e4r',database = 'train')
sec_buildings = pd.read_sql("select * from sec_buildings", con = bridge)
bridge.close()
#用.方法筛选出列
sec_buildings.head()
sec_buildings.name
sec_buildings.price_unit

sec_buildings.size
#用索引的方法筛选出单列
sec_buildings['size']
#用索引的方法筛选出多列
sec_buildings[['name','tot_amt','price_unit']]

#用loc方法加条件筛选出行
sec_buildings.loc[sec_buildings.region == '浦东',:]
#用loc方法加多个条件筛选出行
sec_buildings.loc[(sec_buildings.region == '浦东') & (sec_buildings['size'] > 150),:]
#用loc方法加多个条件筛选出行和列
sec_buildings.loc[(sec_buildings.region == '浦东') & (sec_buildings['size'] > 150),['name','tot_amt','price_unit']]

你可能感兴趣的:(第九章:Pandas模块的介绍-数据子集的筛选)