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

Python usage of len() to get number of array indexes

This is regarding a simple Python function. How come I can read the values in the array x, but not get the number of indexes, even though they are correctly stored in the array.

import pandas as pd
df = pd.ExcelFile('empty_book-kopi.xlsx').parse('Sheet 1')
x=[]
x.append(df['Numbers'])
print("Print method says  :  ")
print(str(x))
print('\n')
print("Lenght method says : ")
print(str(len(x)))

This is the Excel column that I’m importing from and the terminal output:

Terminal_output
Excel_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

>Solution :

If you append df['Numbers'] to the list x, the list will contain a single element and that element will be the Series named Numbers. Instead, you want to include the items in that Series so you need to use the list.extend method.


In [11]: x = []

In [12]: x.append(df['Numbers'])

In [13]: len(x)
Out[13]: 1

In [14]: x.clear()

In [15]: x.extend(df['Numbers'])

In [16]: len(x)
Out[16]: 3
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