[英]How to convert Pandas read excel dataframe to a list in Python?
I'm reading a single column from an Excel file using Pandas:我正在使用 Pandas 从 Excel 文件中读取单个列:
df = pandas.read_excel(file_location, usecols=columnA)
and I want to convert that dataframe (df) into a list.我想将 dataframe (df) 转换成一个列表。 I'm trying to do the following:
我正在尝试执行以下操作:
listA = df.values()
but I'm getting the following error: TypeError: 'numpy.ndarray' object is not callable .但我收到以下错误: 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?
我该怎么做才能解决这个错误,或者有什么其他方法可以将 dataframe 转换成列表? Thank you!
谢谢!
remove the parenthesis from your statement.从您的陈述中删除括号。 with the parens on there, it is treating values like a function.
有了括号,它正在处理像 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]
You should use tolist as follows:您应该按如下方式使用 tolist:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.