首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Python干货:panda特殊索引器——过滤数据

迄今为止最常见从DataFrame获取元素、行和列的数据索引方式:

Dataframe.[];此函数也称为索引运算符。

Dataframe.loc[] :此函数用于标签。

Dataframe.iloc[] :此函数用于基于位置或整数的

Dataframe.ix[] :此函数用于标号和基于整数的函数。

它们统称为索引器。而布尔索引是一种索引类型,它根据DataFrame中数据的实际值而不是它们的行/列标签或整数位置来选择数据子集。

布尔索引中使用布尔向量过滤数据,通过四种方式过滤数据:

使用布尔索引访问DataFrame

将布尔掩码应用于数据帧

基于列值的掩蔽数据

基于索引值的掩蔽数据

使用布尔索引访问DataFrame:

创建一个dataframe,其中的dataframe索引包含一个布尔值。即“True”或“false”。例如

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

df = pd.DataFrame(dict, index = [True, False, True, False])

print(df)

产出:

借助布尔索引访问数据,使用以下三个函数访问数据文件.loc[], .iloc[], .ix[]

使用布尔索引访问Dataframe.loc[]

为了访问具有布尔索引的数据,使用.loc[],我们只需将布尔值(真或假)传递给.loc[]功能。

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

# creating a dataframe with boolean index

df = pd.DataFrame(dict, index = [True, False, True, False])

# accessing a dataframe using .loc[] function

print(df.loc[True])

产出:

使用布尔索引访问Dataframe.iloc[]

为了访问数据文件,请使用.iloc[],我们必须在iloc[]功能但iloc[]函数只接受整数作为参数,因此它将引发一个错误,因此我们只能在将整数传递给iloc[]功能

代码1:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

# creating a dataframe with boolean index

df = pd.DataFrame(dict, index = [True, False, True, False])

# accessing a dataframe using .iloc[] function

print(df.iloc[True])

产出:

TypeError

代码2:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

# creating a dataframe with boolean index

df = pd.DataFrame(dict, index = [True, False, True, False])

# accessing a dataframe using .iloc[] function

print(df.iloc[1])

产出:

使用布尔索引访问Dataframe.ix[]

使用.ix[]访问数据文件,.ix[]函数是.loc[]和.iloc[]功能,将布尔值(真或假)和整数值传递给.ix[]。

代码1:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

# creating a dataframe with boolean index

df = pd.DataFrame(dict, index = [True, False, True, False])

# accessing a dataframe using .ix[] function

print(df.ix[True])

产出:

代码2:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

# creating a dataframe with boolean index

df = pd.DataFrame(dict, index = [True, False, True, False])

# accessing a dataframe using .ix[] function

print(df.ix[1])

产出:

将布尔掩码应用于dataframe:

应用一个布尔掩码,它将只打印传递布尔值True的数据,使用__getitems__或[]访问。

用dataframe中包含的长度相同的真假列表来应用布尔掩码,

代码1:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["MBA", "BCA", "M.Tech", "MBA"],

'score':[90, 40, 80, 98]}

df = pd.DataFrame(dict, index = [0, 1, 2, 3])

print(df[[True, False, True, False]])

产出:

代码2:

# importing pandas package

import pandas as pd

# making data frame from csv file

data = pd.read_csv("nba1.1.csv")

df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,

7, 8, 9, 10, 11, 12])

df[[True, False, True, False, True,

False, True, False, True, False,

True, False, True]]

产出:

基于列值的掩蔽数据:

在dataframe中,使用不同的运算符(如==, >, 

代码1:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["BCA", "BCA", "M.Tech", "BCA"],

'score':[90, 40, 80, 98]}

# creating a dataframe

df = pd.DataFrame(dict)

# using a comparison operator for filtering of data

print(df['degree'] == 'BCA')

产出:

代码2:

# importing pandas package

import pandas as pd

# making data frame from csv file

data = pd.read_csv("nba.csv", index_col ="Name")

# using greater than operator for filtering of data

print(data['Age'] > 25)

产出:

基于索引值的掩蔽数据:

在dataframe中,使用不同的运算符创建基于索引值的掩码 ==, >, 

代码1:

# importing pandas as pd

import pandas as pd

# dictionary of lists

dict = {'name':["aparna", "pankaj", "sudhir", "Geeku"],

'degree': ["BCA", "BCA", "M.Tech", "BCA"],

'score':[90, 40, 80, 98]}

df = pd.DataFrame(dict, index = [0, 1, 2, 3])

mask = df.index == 0

print(df[mask])

产出:

代码2:

# importing pandas package

import pandas as pd

# making data frame from csv file

data = pd.read_csv("nba1.1.csv")

# giving a index to a dataframe

df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, 6,

7, 8, 9, 10, 11, 12])

# filtering data on index value

mask = df.index > 7

df[mask]

产出:

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20201117A0AWH800?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券