Pandas系列教程(3)Pandas数据查询

2023-02-28,,

pandas 查询数据的几种方法

  1. df.loc方法,根据行,列的标签值查询

  2. df.iloc方法,根据行,列的数字位置查询

  3. df.where方法

  4. df.query方法

.loc即可以查询,又能覆盖雪茹,强烈推荐

pandas 使用df.loc查询数据的方法

  1. 使用单个label值查询数据

  2. 使用值列表批量查询

  3. 使用数值区间进行范围查询

  4. 使用条件表达式查询

  5. 调用函数查询

注意:

  • 以上查询方法,即适用于行,也适用于列

  • 注意观察降维DataFrame>Series>值

1、读取数据

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)

print("打印前几行的数据:\n ", df.head())

# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)

# 时间序列见后续课程,本次按字符串处理
print("打印索引:\n ", df.index)

print("打印前几行的数据:\n ", df.head())

# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

print("打印每列的数据类型:\n ", df.dtypes)

print("打印前几行的数据:\n ", df.head())

 

2、使用单个label值查询数据

 行或者列,都可以只传单个值,实现精确匹配

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 打印前几行数据
print(df.head())

# 得到单个值(获取2018-01-03的最高温度)
print(df.loc['2018-01-03', 'bWendu'])

# 得到一个Series(获取2018-01-03的最高温度和最低温度)
print(df.loc['2018-01-03', ['bWendu', 'yWendu']])

 

3、使用值列表批量查询

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 打印前几行数据
print(df.head())

# 得到Series(获取['2018-01-03', '2018-01-04', '2018-01-05']的最高温度)
print(df.loc[['2018-01-03', '2018-01-04', '2018-01-05'], 'bWendu'])

# 得到DataFrame(获取['2018-01-03', '2018-01-04', '2018-01-05']的最高温度和最低温度)
print(df.loc[['2018-01-03', '2018-01-04', '2018-01-05'], ['bWendu', 'yWendu']])

 

4、使用数值区间进行范围查询

 注意:区间即包含开始,也包含结束

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 打印前几行数据
print(df.head(), '\n', '*' * 50)

# 行index按区间
print(df.loc['2018-01-03':'2018-01-05', 'bWendu'], '\n', '*' * 50)

# 列index按区间
print(df.loc['2018-01-03', 'bWendu':'fengxiang'], '\n', '*' * 50)

# 行列都按区间查询
print(df.loc['2018-01-03':'2018-01-05', 'bWendu':'fengxiang'])

 

5、使用条件表达式查询

 bool列表的长度等于行数或者列数

简单条件查询

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 简单查询, 最低温度低于-10度的列表
print(df.loc[df['yWendu'] < -10, :], '\n', '*' * 50)

# 观察一下这里的boolean条件
print(df['yWendu'] < -10, '\n', '*' * 50)

复杂条件查询

注意:组合条件用&符号合并,每个条件判断都得带括号

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 查询最高温度小于30度,并且最低温度大于十五度,并且是晴天,并且天气为优的数据
print(df.loc[(df['bWendu'] <= 30) & (df['yWendu'] >= 15) & (df['tianqi'] == '') & (df['aqiLevel'] == 1), :])
print('*' * 50)

# 观察一下这里的boolean条件
print((df['bWendu'] <= 30) & (df['yWendu'] >= 15) & (df['tianqi'] == '') & (df['aqiLevel'] == 1))

 

6、调用函数查询

import pandas as pd

file_path = "../files/beijing_tianqi_2018.csv"
df = pd.read_csv(file_path)
# 数据的预处理
# 设定索引为日期,方便按日期筛选
df.set_index('ymd', inplace=True)
# 替换温度的后缀℃
df.loc[:, 'bWendu'] = df.loc[:, 'bWendu'].str.replace('', '').astype('int32')
df.loc[:, 'yWendu'] = df.loc[:, 'yWendu'].str.replace('', '').astype('int32')

# 直接写lambda表达式
print(df.loc[lambda df: (df['bWendu'] <= 30) & (df['yWendu'] >= 15), :])
print('*' * 50)

# 编写自己的函数,查询9月份,空气质量好的数据
def query_my_data(df):
    return df.index.str.startswith('2018-09') & df['aqiLevel'] == 1
print(df.loc[query_my_data, :])