我正在使用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=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()
我的代码中出现了一些错误
获取Dataframe中的记录总数
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
另一个疑问,如何访问二维数据帧中的值。 df [[0,1]]不起作用
示例日期
任何人都可以帮我解决错误。 谢谢!
答案 0 :(得分:2)
函数np.array
必须作用于对象,因此调用不带任何参数的函数会导致错误。有效的替代方案是np.zeros(0)
和np.empty(0).
语法df[1].count()
应替换为df.iloc[:,1].count()
。