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

The correct way to get the size of a list

I have a dataframe with one column and one row like:

list_id
12,13,14,16

The list_id column type is Object:

df['list'].dtypes => Object

When I try to get the number of elements in this column

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

l = list(df.list_id)

len(l) => 1

Or

len(df['list_id'] => 1

Why I am getting 1 instead of 4?

I want to get the count of elements as 4. What Can I do?

>Solution :

Example

data = {'list_id': {0: '12,13,14,16'}} 
df = pd.DataFrame(data)

Code

df['list_id'].str.split(',').str.len()

Result

0    4
Name: list_id, dtype: int64

if you want get only 4 not series, use following code:

df['list_id'].str.split(',').str.len()[0]
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