【问题标题】:Filtering a Pandas DataFrame Using a Function使用函数过滤 Pandas DataFrame
【发布时间】:2020-11-29 07:00:30
【问题描述】:

这个问题和我昨天发的问题有关,可以找到here

因此,我继续将 Jan 提供的解决方案应用于整个数据集。解决方法如下:

import re

def is_probably_english(row, threshold=0.90):
    regular_expression = re.compile(r'[-a-zA-Z0-9_ ]')
    ascii = [character for character in row['App'] if regular_expression.search(character)]
    quotient = len(ascii) / len(row['App'])
    passed = True if quotient >= threshold else False
    return passed

google_play_store_is_probably_english = google_play_store_no_duplicates.apply(is_probably_english, axis=1)

google_play_store_english = google_play_store_no_duplicates[google_play_store_is_probably_english]

因此,据我了解,我们正在使用 is_probably_english 函数过滤 google_play_store_no_duplicates DataFrame,并将结果(布尔值)存储到另一个 DataFrame (google_play_store_is_probably_english) 中。然后使用 google_play_store_is_probably_english 过滤掉 google_play_store_no_duplicates DataFrame 中的非英语应用程序,并将最终结果存储在新的 DataFrame 中。

这有意义吗?这似乎是解决问题的好方法吗?有没有更好的方法来做到这一点?

【问题讨论】:

    标签: python pandas dataframe data-science


    【解决方案1】:

    这是有道理的,我认为这是最好的方法,函数的结果是你所说的布尔值,然后当你将它应用到 pd.Series 中时,你最终会得到一个 pd.Series 的布尔值,通常称为布尔掩码。当您想通过某些参数过滤行时,这个概念在 pandas 中非常有用。

    Here 是一篇关于 pandas 中布尔掩码的文章。

    【讨论】:

      猜你喜欢
      • 2013-07-30
      • 2022-11-15
      • 2014-02-06
      • 2017-10-08
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多