获取Pandas DataFrame中包含给定子字符串的所有记录

获取Pandas DataFrame中包含给定子字符串的所有记录

让我们看看如何在不同的例子的帮助下,获得Pandas DataFrame中包含给定子字符串的所有行。

代码#1:检查Position列中的PG值

# importing pandas 
import pandas as pd
  
# Creating the dataframe with dict of lists
df = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'],
                   'Team': ['Boston', 'Boston', 'Boston', 'Chele', 'Barse'],
                   'Position': ['PG', 'PG', 'UG', 'PG', 'UG'],
                   'Number': [3, 4, 7, 11, 5],
                   'Age': [33, 25, 34, 35, 28],
                   'Height': ['6-2', '6-4', '5-9', '6-1', '5-8'],
                   'Weight': [89, 79, 113, 78, 84],
                   'College': ['MIT', 'MIT', 'MIT', 'Stanford', 'Stanford'],
                   'Salary': [99999, 99994, 89999, 78889, 87779]},
                   index =['ind1', 'ind2', 'ind3', 'ind4', 'ind5'])
print(df, "\n")
  
print("Check PG values in Position column:\n")
df1 = df['Position'].str.contains("PG")
print(df1)

输出:
获取Pandas DataFrame中包含给定子字符串的所有记录

但是这个结果似乎没有什么帮助,因为它返回的是带有索引的bool值。让我们看看我们是否可以做得更好。

代码#2:获得满足条件的行

# importing pandas as pd
import pandas as pd
  
# Creating the dataframe with dict of lists
df = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'],
                   'Team': ['Boston', 'Boston', 'Boston', 'Chele', 'Barse'],
                   'Position': ['PG', 'PG', 'UG', 'PG', 'UG'],
                   'Number': [3, 4, 7, 11, 5],
                   'Age': [33, 25, 34, 35, 28],
                   'Height': ['6-2', '6-4', '5-9', '6-1', '5-8'],
                   'Weight': [89, 79, 113, 78, 84],
                   'College': ['MIT', 'MIT', 'MIT', 'Stanford', 'Stanford'],
                   'Salary': [99999, 99994, 89999, 78889, 87779]},
                   index =['ind1', 'ind2', 'ind3', 'ind4', 'ind5'])
  
df1 = df[df['Position'].str.contains("PG")]
print(df1)

输出:
获取Pandas DataFrame中包含给定子字符串的所有记录

代码#3:过滤所有团队包含 “波士顿 “或学院包含 “麻省理工 “的行。

# importing pandas
import pandas as pd
  
# Creating the dataframe with dict of lists
df = pd.DataFrame({'Name': ['Geeks', 'Peter', 'James', 'Jack', 'Lisa'],
                   'Team': ['Boston', 'Boston', 'Boston', 'Chele', 'Barse'],
                   'Position': ['PG', 'PG', 'UG', 'PG', 'UG'],
                   'Number': [3, 4, 7, 11, 5],
                   'Age': [33, 25, 34, 35, 28],
                   'Height': ['6-2', '6-4', '5-9', '6-1', '5-8'],
                   'Weight': [89, 79, 113, 78, 84],
                   'College': ['MIT', 'MIT', 'MIT', 'Stanford', 'Stanford'],
                   'Salary': [99999, 99994, 89999, 78889, 87779]},
                   index =['ind1', 'ind2', 'ind3', 'ind4', 'ind5'])
  
  
df1 = df[df['Team'].str.contains("Boston") | df['College'].str.contains('MIT')]
print(df1)

输出:
获取Pandas DataFrame中包含给定子字符串的所有记录

代码#4:过滤行,检查球队名称是否包含 “波士顿”,位置是否为PG。

# importing pandas module 
import pandas as pd 
    
# making data frame 
df = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") 
  
  
df1 = df[df['Team'].str.contains('Boston') & df['Position'].str.contains('PG')]
df1

输出:
获取Pandas DataFrame中包含给定子字符串的所有记录

代码#5:过滤行,检查Position是否包含PG,College是否必须包含UC。

# importing pandas module 
import pandas as pd 
    
# making data frame 
df = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv") 
  
  
df1 = df[df['Position'].str.contains("PG") & df['College'].str.contains('UC')]
df1

输出:
获取Pandas DataFrame中包含给定子字符串的所有记录

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程