Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to count the number of commas within a string column (Python)

I have an Excel file that includes a column originally stores dictionary-type data look like below:

I imported it into PD frame and I plan to count the number of elements for each row and save the result as another output which represents the number of elements.

Since there’s no clear pattern for the elements where some elements are quoted and others are not, my strategy is to just count the number of commas.

I’ve been searching for a good code to address this for a few hours..Any help would be greatly appreciated…

Have

index dic
0     {a,"b",c,d,"d"}
1     {"e","f",g,h,"j",l}
...
Want

index dic                  num_element
0     {a,"b",c,d,"d"}      5
1     {"e","f",g,h,"j",l}  6
...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming you have strings, you could use:

df['num_element'] = df['dic'].str.count(',').add(1)

Output:

   index                  dic  num_element
0      0      {a,"b",c,d,"d"}            5
1      1  {"e","f",g,h,"j",l}            6
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading