Jaime Miranda Ramirez
Jaime Miranda Ramirez

Reputation: 315

How to convert Pandas read excel dataframe to a list in Python?

I'm reading a single column from an Excel file using Pandas:

df = pandas.read_excel(file_location, usecols=columnA)

and I want to convert that dataframe (df) into a list. I'm trying to do the following:

listA = df.values()

but I'm getting the following error: TypeError: 'numpy.ndarray' object is not callable. What can I do to solve this error or is there any other way I can convert that dataframe into a list? Thank you!

Upvotes: 1

Views: 2285

Answers (2)

RamiN
RamiN

Reputation: 11

You should use tolist as follows:

import pandas as pd

data = pd.read_excel(file_location, sheet_name="data")
list_data =pd.DataFrame(data,columns['C1','C2','C3','C4','C5']).values.tolist()

Upvotes: -1

AirSquid
AirSquid

Reputation: 11883

remove the parenthesis from your statement. with the parens on there, it is treating values like a function. It is an instance variable:

listA = df.values     # note no parenthesis after values

Here are a couple ideas. You should probably access the column by name

In [2]: import pandas as pd                                                     

In [3]: df = pd.DataFrame({'A':[1,5,99]})                                       

In [4]: df                                                                      
Out[4]: 
    A
0   1
1   5
2  99

In [5]: df.values                                                               
Out[5]: 
array([[ 1],
       [ 5],
       [99]])

In [6]: my_list = list(df['A'])                                                 

In [7]: my_list                                                                 
Out[7]: [1, 5, 99]

Upvotes: 2

Related Questions