13

Let's say I have a dataframe that looks like this:

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

Is their a way to count the number of occurrences of a specific character or set of characters?

i.e. I want to count the number of "-" and "/" and add them together, so my output my look like this:

                      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

1 Answer 1

23

Pass a regular expression to str.count (| is used for or):

df2['A'].str.count('/|-')
Out: 
0    3
1    2
2    2
3    2
4    4
5    3
6    1
Name: A, dtype: int64
1
  • I had to convert my "A" column to string first (.astype(str))
    – Henrik
    Commented Nov 21, 2022 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.