gpt4 book ai didi

python - 如何使用python查找数据框中的空格数

转载 作者:行者123 更新时间:2023-12-04 14:48:59 25 4
gpt4 key购买 nike

我有一个包含几列的 excel 文件,我需要在列值中找到 no.of 空格。

例子:

<表类="s-表"><头>键<正文>标识符标识符号用户标识号用户用户位置
  df['count'] =df['Key'].apply(lambda x:x.isspace())

上一行给出了 True、False 值。谁能帮我计算“键”列名称中的空格数。

最佳答案

使用Series.str.count使用 \s+ 匹配空格:

df['count'] =df['Key'].str.count('\s+')
print (df)
Key count
0 Identifier 0
1 Identifier Number 1
2 user identifier number 2
3 user 0
4 Location of user 2

关于python - 如何使用python查找数据框中的空格数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69370793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com