【问题标题】:To count how many cells are striped in a column计算一列中有多少个单元格被条带化
【发布时间】:2021-01-12 00:44:34
【问题描述】:
aw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
        'last_name': [" 'Miller' "," 'Jacobson' ", 'Ali', 'Milner', 'Cooze'], 
        'age': [42, 52, 36, 24, 73], 
        'preTestScore': [4, 24, 31, 2, 3],
        'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
df

我的任务是首先删除姓氏列中的引号,并计算该列中有多少单元格在此过程中被删除。

我的工作: 我可以去掉姓氏列中的引号,但我该如何计算它。 我正在使用:

df["last_name"]=df["last_name"].apply(lambda x: x.replace("'",""))

我的输出应该是“二”。 任何帮助表示赞赏。

【问题讨论】:

  • df['last_name'].str.contains("'").sum() ?
  • @anky 这也将计算其中包含 ' 的名称(但 OP 的 .replace 也是如此)。这可能是一个问题,也可能不是。可以使用str.extract
  • 可以使用str.count, df["last_name"].str.count("'")

标签: python-3.x pandas dataframe data-mining data-processing


【解决方案1】:

计算该列中有多少单元格在此过程中被剥离

你可以使用:

df['last_name'].str.split("'").str[1:-1].str.len().ne(0).sum()

要查看我的评论与此之间的区别,请考虑以下示例:

raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], 
    'last_name': [" 'Miller's' "," 'Jacobson's' ", 'Ali', 'Milner', "Cooze's"], 
    'age': [42, 52, 36, 24, 73], 
    'preTestScore': [4, 24, 31, 2, 3],
    'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = 
    ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
print(df)

  first_name       last_name  age  preTestScore  postTestScore
0      Jason     'Miller's'    42             4             25
1      Molly   'Jacobson's'    52            24             94
2       Tina             Ali   36            31             57
3       Jake          Milner   24             2             62
4        Amy         Cooze's   73             3             70

你可以使用:

df['last_name'].str.split("'").str[1:-1].str.len().ne(0).sum()
#2 since the last_name `Cooze's` contains an apostrophe but not quoted.

鉴于:

df['last_name'].str.contains("'").sum()
#3 since it counts all rows having an apostrophe

【讨论】:

  • 这很有趣 :) +1
【解决方案2】:

你可以试试str.findallsum

In [99]: df.last_name.str.findall(r"^ *\'|\' *$").astype(bool).sum()
Out[99]: 2

关于修改后的样本df:

  first_name        last_name  age  preTestScore  postTestScore
0      Jason        'Miller'    42             4             25
1      Molly       Jacobson'    52            24             94
2       Tina             Ali'   36            31             57
3       Jake         Milner's   24             2             62
4        Amy  Cooze             73             3             70

In [106]: df.last_name.str.findall(r"^ *\'|\' *$").astype(bool).sum()
Out[106]: 3

【讨论】:

  • 似乎我们有相似的想法,但有点不同,我可以使用 astype bool 吗? :P
  • @anky: 当然,兄弟 :))
  • 我想没关系,很好地使用 bool,我应该使用它而不是检查 len()!=0
【解决方案3】:

你可以试试下面的-

df[df["last_name"].str.contains("'")].count()["last_name"]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2016-03-01
    相关资源
    最近更新 更多