【问题标题】:How to filter on pandas dataframe when column data type is a list当列数据类型为列表时如何过滤熊猫数据框
【发布时间】:2017-02-05 08:36:37
【问题描述】:

我在过滤数据类型为列表的列(我们称之为 column_1)上的 pandas 数据框时遇到了一些问题。具体来说,我只想返回 column_1 和另一个预定列表的交集不为空的行。但是,当我尝试将逻辑放入 .where 函数的参数中时,我总是会出错。以下是我的尝试,返回了错误。

  • 尝试测试单个元素是否在列表中:

    table[element in table['column_1']] 返回错误... KeyError: False

  • 尝试将列表与数据框行中的所有列表进行比较:

    table[[349569] == table.column_1] 返回错误Arrays were different lengths: 23041 vs 1

在测试这两个列表的交集之前,我正在尝试降低这两个中间步骤。

感谢您花时间阅读我的问题!

【问题讨论】:

    标签: list pandas dataframe filter where


    【解决方案1】:

    您好,对于长期使用,您可以将整个工作流程封装在函数中,并将函数应用到您需要的地方。因为您没有放置任何示例数据集。我正在获取一个示例数据集并解决它。考虑到我有文本数据库。首先我会在列表中找到#tags,然后我会搜索我想要的唯一#tags 并过滤数据。

    # find all the tags in the message
    def find_hashtags(post_msg):
        combo = r'#\w+'
        rx = re.compile(combo)
        hash_tags = rx.findall(post_msg)
        return hash_tags
    
    
    # find the requered match according to a tag list and return true or false
    def match_tags(tag_list, htag_list):
        matched_items = bool(set(tag_list).intersection(htag_list))
        return matched_items
    
    
    test_data = [{'text': 'Head nipid mõnusateks sõitudeks kitsastel tänavatel. #TipStop'},
     {'text': 'Homses Rooli Võimus uus #Peugeot208!\nVaata kindlasti.'},
     {'text': 'Soovitame ennast tulevikuks ette valmistada, electric car sest uus #PeugeotE208 on peagi kohal!  ⚡️⚡️\n#UnboringTheFuture'},
     {'text': "Aeg on täiesti uueks roadtrip'i kogemuseks! \nLase ennast üllatada - #Peugeot5008!"},
     {'text': 'Tõeline ikoon, mille stiil avaldab muljet läbi eco car, electric cars generatsioonide #Peugeot504!'}
    ]
    
    test_df = pd.DataFrame(test_data)
    
    # find all the hashtags
    test_df["hashtags"] = test_df["text"].apply(lambda x: find_hashtags(x))
    
    # the only hashtags we are interested
    tag_search = ["#TipStop", "#Peugeot208"]
    
    # match the tags in our list
    test_df["tag_exist"] = test_df["hashtags"].apply(lambda x: match_tags(x, tag_search))
    
    # filter the data
    main_df = test_df[test_df.tag_exist]
    

    【讨论】:

      【解决方案2】:

      考虑pd.Seriess

      s = pd.Series([[1, 2, 3], list('abcd'), [9, 8, 3], ['a', 4]])
      print(s)
      
      0       [1, 2, 3]
      1    [a, b, c, d]
      2       [9, 8, 3]
      3          [a, 4]
      dtype: object
      

      还有一个测试列表test

      test = ['b', 3, 4]
      

      应用lambda 函数,将s 的每个元素转换为集合,并将intersection 转换为test

      print(s.apply(lambda x: list(set(x).intersection(test))))
      
      0    [3]
      1    [b]
      2    [3]
      3    [4]
      dtype: object
      

      要将其用作掩码,请使用bool 而不是list

      s.apply(lambda x: bool(set(x).intersection(test)))
      
      0    True
      1    True
      2    True
      3    True
      dtype: bool
      

      【讨论】:

        猜你喜欢
        • 2021-12-01
        • 2017-12-15
        • 2020-08-08
        • 2022-10-04
        • 2014-12-02
        • 2019-05-21
        • 1970-01-01
        • 2019-05-21
        • 2018-07-13
        相关资源
        最近更新 更多