在Python中使用Dataframe和numpy数组时出错
我正在使用pandas Dataframe和numpy数组,在访问Dataframe中的数据和在计算中使用数据时遇到问题 我的代码:在Python中使用Dataframe和numpy数组时出错,python,pandas,numpy,dataframe,Python,Pandas,Numpy,Dataframe,我正在使用pandas Dataframe和numpy数组,在访问Dataframe中的数据和在计算中使用数据时遇到问题 我的代码: import pypyodbc import numpy as np import pandas as pd connection = pypyodbc.connect('Driver={SQL Server};''Server=GIRSQL.GIRCAPITAL.com;''Database=Tableau;''uid=SQL_User;pwd
import pypyodbc
import numpy as np
import pandas as pd
connection = pypyodbc.connect('Driver={SQL Server};''Server=GIRSQL.GIRCAPITAL.com;''Database=Tableau;''uid=SQL_User;pwd=Greentableau!')
cursor = connection.cursor()
SQLCommand = ("SELECT * FROM [Tableau].[dbo].[Country_table3$]")
df = pd.read_sql_query(SQLCommand, connection)
real_earnings = np.array([])
real_price = np.array([])
CAPE = np.array([])
print(df)
total_rows = df[1].count()
print("total rows:" + total_rows)
CPI_latest = df[8,total_rows]
i = 0
for i in total_rows:
real_earnings[i] = df[[2,i]] * CPI_latest/df[[8,i]]
real_price[i] = df[[3,i]] * CPI_latest/df[[8,i]]
connection.close()
我的代码中有几个错误
total_rows = df[1].count()
错误:
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 132, in
pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5280)
File "pandas\_libs\index.pyx", line 154, in
pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5126)
File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in
pandas._libs.hashtable.PyObjectHashTable.get_item
(pandas\_libs\hashtable.c:20523)
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in
pandas._libs.hashtable.PyObjectHashTable.get_item
(pandas\_libs\hashtable.c:20477)
KeyError: 1
谢谢 函数
np.array
必须作用于对象,因此在没有任何参数的情况下调用函数会导致错误。有效的选择是np.zero(0)
和np.empty(0)。
语法
df[1].count()
应替换为df.iloc[:,1].count()
签出numpy.empty()
仍收到相同的错误消息您签出文档了吗?这就是我的意思…@ArvinthKumar如果没有任何样本数据,我们怎么能提供帮助?关于这一点有很多问题,请参考您看到的KeyError
。