Home > other >  Python usage of len() to get number of array indexes
Python usage of len() to get number of array indexes

Time:07-18

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

CodePudding user response:

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
  • Related