Home > OS >  Count the occurrences of each character in a alpha-numeric column in a DataFrame
Count the occurrences of each character in a alpha-numeric column in a DataFrame

Time:11-12

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.

e.g.

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