Count string lengths in pandas dataframe column

Viewed 527

I am exploring a large dataframe with an object (string) column (air) of varying lengths such as this small example.

aic
12345678
87654321
123456789
1234

I want to obtain a summary of the count of each string length such as for the example:

length  count
4       1
8       2
9       1

I tried with

df["aic"].str.len().nunique()

which returns 3 meaning there are two different lengths but I don't know which.

Thank you

1 Answers

You can do

out = df.aic.astype(str).str.len().value_counts().reset_index()
Out[104]: 
   index  aic
0      8    2
1      9    1
2      4    1
Related