1

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!

3
  • can you show the dataframe you are trying to convert ? Commented Jun 4, 2020 at 19:52
  • 1
    df.values.tolist() Commented Jun 4, 2020 at 19:53
  • That solution worked, thank you @Stef I didn't know there was a .tolist() Commented Jun 4, 2020 at 19:56

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

Comments

-1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.