Python Pandas Series.filter()

Python Pandas Series.filter()

Pandas系列是一个带有轴标签的一维ndarray。标签不需要是唯一的,但必须是一个可散列的类型。该对象支持基于整数和标签的索引,并提供了大量的方法来执行涉及索引的操作。

Pandas Series.filter()函数根据指定索引中的标签返回数据框架的子集行或列。请注意,这个程序不会对数据框架的内容进行过滤。过滤器被应用到索引的标签上。

语法: Series.filter(items=None, like=None, regex=None, axis=None)

参数:
items :要限制的轴的列表(必须不全部存在)。
like:保留 “arg in col == True “的轴。
regex :保持轴与re.search(regex, col) == True。
axis :要过滤的轴。默认情况下,这是信息轴,”索引 “用于系列,”列 “用于数据框架。

返回:与输入对象的类型相同

例子#1:使用Series.filter()函数,用正则表达式过滤掉给定系列对象中的一些值。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([80, 25, 3, 25, 24, 6])
  
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

输出 :
Python Pandas Series.filter()

现在我们将使用Series.filter()函数从给定的系列对象中过滤那些索引标签名称中有空格的值。

# filter values
result = sr.filter(regex = '. .')
  
# Print the result
print(result)

输出 :
Python Pandas Series.filter()
正如我们在输出中看到的,Series.filter()函数已经成功地从给定的系列对象中返回了所需的值。

示例#2 :使用Series.filter()函数,使用索引标签列表过滤掉给定系列对象中的一些数值。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio'])
  
# Create the Index
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5'] 
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

输出 :
Python Pandas Series.filter()
现在我们将使用Series.filter()函数来过滤给定系列对象中与传递的索引标签对应的值。

# filter values
result = sr.filter(items = ['City 2', 'City 4'])
  
# Print the result
print(result)

输出 :
Python Pandas Series.filter()
正如我们在输出中看到的,Series.filter()函数已经成功地从给定的系列对象中返回了所需的值。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程