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
...
CodePudding user response:
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