Home > Mobile >  Python- Count the occurrences of each character in a alpha-numeric column in a dataframe
Python- Count the occurrences of each character in a alpha-numeric column in a dataframe

Time:11-11

I have a alpha-numeric column in a dataframe. I would like get the total count of each characters(0-9, A-Z) occurs in the entire column

Eg.

Serial 03000395 A000458B 667BC345

Desired Output Character Counts 0 7 1 0 2 0 3 3 . . A 1 B 2 C 1 .. Z

CodePudding user response:

You can use Counter to get this

from collections import Counter
Counter('03000395 A000458B 667BC345)

Output:

Counter({'0': 7,
         '3': 3,
         '9': 1,
         '5': 3,
         ' ': 2,
         'A': 1,
         '4': 2,
         '8': 1,
         'B': 2,
         '6': 2,
         '7': 1,
         'C': 1})

EDIT: After clarifying comments about this being in a dataframe you can do this:

pd.DataFrame(df.Serial.value_counts()).reset_index().rename({'index':'Character','Serial':'Count'})
  • Related