Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用Dataframe和numpy数组时出错_Python_Pandas_Numpy_Dataframe - Fatal编程技术网

在Python中使用Dataframe和numpy数组时出错

在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

我正在使用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]]不工作

    样本日期

    谁能帮我解决这些错误吗。
    谢谢

    函数
    np.array
    必须作用于对象,因此在没有任何参数的情况下调用函数会导致错误。有效的选择是
    np.zero(0)
    np.empty(0)。


    语法
    df[1].count()
    应替换为
    df.iloc[:,1].count()

    签出
    numpy.empty()
    仍收到相同的错误消息您签出文档了吗?这就是我的意思…@ArvinthKumar如果没有任何样本数据,我们怎么能提供帮助?关于这一点有很多问题,请参考您看到的
    KeyError