獲取包含給定子字符串的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) 

輸出:

但是這個結果似乎並沒有太大幫助,因為它返回帶有索引的布爾值。讓我們看看是否可以做得更好。
 

代碼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) 

輸出:

 

代碼3:過濾Team包含‘Boston’或大學包含“ MIT”的所有行。

# 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) 

輸出:


 
代碼4:篩選行,檢查Team名稱是否包含‘Boston’,Position必須為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 

輸出:


 

代碼5: Filter rows checking Position contains PG and College must contains like 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 

輸出:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM