python - Pandas 计算字符出现次数

标签 python python-3.x pandas

假设我有一个如下所示的数据框:

df2 = pd.DataFrame(['2018/10/02, 10/2', '02/20/18', '10-31/2018', '1111-0-1000000', '2018/10/11/2019/9999', '10-2, 11/2018/01', '10/2'], columns=['A'])

>>> df2

                      A
0      2018/10/02, 10/2
1              02/20/18
2            10-31/2018
3        1111-0-1000000
4  2018/10/11/2019/9999
5      10-2, 11/2018/01
6                  10/2

它们是计算特定字符或字符集出现次数的方法吗?

即我想计算“-”和“/”的数量并将它们加在一起,所以我的输出看起来像这样:

                      A     specific_character_count
0      2018/10/02, 10/2                            3
1              02/20/18                            2
2            10-31/2018                            2
3        1111-0-1000000                            2
4  2018/10/11/2019/9999                            4
5      10-2, 11/2018/01                            3
6                  10/2                            1

最佳答案

将正则表达式传递给str.count(|用于):

df2['A'].str.count('/|-')
Out: 
0    3
1    2
2    2
3    2
4    4
5    3
6    1
Name: A, dtype: int64

关于python - Pandas 计算字符出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52010772/

相关文章:

python-3.x - 如何使用 Python Pandas 将列的日期聚合到每个人的日期列表中?

python - 为每个 Pandas 单元格设置相同的字典

python - 将列 reshape 为数据框 Pandas 中的行

Python (Pytorch) 多处理抛出错误 : Connection reset by peer and File Not Found

python - 向图像添加色度噪声

Python argparse 和控制/覆盖退出状态代码

python-3.x - 将时间戳转换为日期到Python中的多列

python - 如何压缩无限范围的迭代器?

python - 如何根据条件对 Pandas DataFrame 进行分组?

python - 如何离线安装 Pytorch?