zoukankan      html  css  js  c++  java
  • 获取包含给定子字符串的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, "
    ") 
    
    print("Check PG values in Position column:
    ") 
    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 

    输出:

  • 相关阅读:
    【已解决】对发现无理数过程的逻辑严谨性的疑惑
    微积分奇观之计算曲线的平均高度
    闲鱼二维码 另外那个号
    联通KD-YUN-811G光猫管理员密码
    人工智能结课作业-BP神经网络/卷积神经网络手写体识别
    人工智能结课作业-遗传算法/粒子群寻优/蚁群算法解决TSP问题
    人工智能结课作业-DFS/BFS/Astar解决八数码问题
    AMD 2020显卡驱动没有切换独立显卡选项
    linux创建文件夹快捷方式
    Ubuntu 18.04 设置开机启动脚本
  • 原文地址:https://www.cnblogs.com/a00ium/p/13875176.html
Copyright © 2011-2022 走看看