我正在try 使用Pandas 访问数据帧中列中的一些单元格. 不幸的是,我收到一个错误,内容是:

/usr/local/lib/python3.10/dist-packages/pandas/core/indexing.py In _getitem_axis(自身,关键帧,轴)1620 key=Item_from_zerodim(关键字)1621如果不是_INTEGER(关键字): ->1622 RAISE TypeError("无法使用非整数键按位置索引编制索引")1623 1624#验证 地点

TypeError:无法使用非整数键按位置索引进行索引

Python代码:

#Calculate the cost, revenue, and profit for each day in each year.
def calculateParametersOnedayEachMonth(ptype,month, year):

  price = 0
  r = 0
  
  data = pd.DataFrame(readData)
  #print(data[["Date"]])

  for x in range(data.size):
    #print(x)
    if data.iloc[[x]["Date"]] == "1/"+month+"/"+str(year)+"12:00:00" :
       print("I am inside")
       index1 = x
       index2 = x+24
       for index1 in range(index2):
         if type == "PV":
           price = price + data.iloc[[index1]['PVPrice']]
           print(price)
           loadForcast = data.iloc[[index1]['PVLoadForecast']]
           powerForcast = data.iloc[[index1]['PVPowerForecast']]
           r = r+(loadForcast-powerForcast)  
           print(r)
         elif type == "DG":
           price = price + data.iloc[[index1]['DGPrices']]
           loadForcast = data.iloc[[index1]['DGLoad forecast']]
           powerForcast = data.iloc[[index1]['DGPowerForecast']]
           r = r+(loadForcast-powerForcast)
         elif type == "Wind":
           price = price + data.iloc[[index1]['WPrice']]
           loadForcast = data.iloc[[index1]['WindLoadforecast']]
           powerForcast = data.iloc[[index1]['WindPowerForecast']]
           r = r+(loadForcast-powerForcast)
         elif type == "BES":
           price = price + data.iloc[[index1]['BESPrice']]
           loadForcast = data.iloc[[index1]['BESLoadForecast']]
           powerForcast = data.iloc[[index1]['BESPowerForecast']]
           r = r+(loadForcast-powerForcast)
         elif type == "Zonal":
           price = price + data.iloc[[index1]['ZonalPrice']]
           loadForcast = data.iloc[[index1]['ZonalLoadForecast']]
           powerForcast = data.iloc[[index1][' ZonalPowerForecast']]
           r = r+(loadForcast-powerForcast)
         else: print("Sorry, this type is not provided by dataset")
       break
   

  avgPrice = price / 24
  avgRevenue = r/24

  print("Day-Ahead Energy Price",avgPrice)
  print("Day-Ahead Revenue",avgPrice)
 


  allValues = {  "Cost": avgPrice , "Reveneu": avgRevenue}

  if type=="PV":
    if year == "2011":
      listAllDaysParameters2011_PV.append(allValues)
    elif year == "2012":
      listAllDaysParameters2012_PV.append(allValues)
    elif year == "2013":
      listAllDaysParameters2013_PV.append(allValues)
    elif year == "2014":
      listAllDaysParameters2014_PV.append(allValues)
    elif year == "2015":
      listAllDaysParameters2015_PV.append(allValues)
    elif year == "2016":
      listAllDaysParameters2016_PV.append(allValues)
    else: print("This year is not provided by the dataset")

  elif type=="DG":
    if year == 2011:
      listAllDaysParameters2011_DG.append(allValues)
    elif year == 2012:
      listAllDaysParameters2012_DG.append(allValues)
    elif year == 2013:
      listAllDaysParameters2013_DG.append(allValues)
    elif year == 2014:
      listAllDaysParameters2014_DG.append(allValues)
    elif year == 2015:
      listAllDaysParameters2015_DG.append(allValues)
    elif year == 2016:
      listAllDaysParameters2016_DG.append(allValues)
    else: print("This year is not provided by the dataset")

  elif type=="Wind":
    if year == 2011:
      listAllDaysParameters2011_Wind.append(allValues)
    elif year == 2012:
      listAllDaysParameters2012_Wind.append(allValues)
    elif year == 2013:
      listAllDaysParameters2013_Wind.append(allValues)
    elif year == 2014:
      listAllDaysParameters2014_Wind.append(allValues)
    elif year == 2015:
      listAllDaysParameters2015_Wind.append(allValues)
    elif year == 2016:
      listAllDaysParameters2016_Wind.append(allValues)
    else: print("This year is not provided by the dataset")

  elif type=="BES":
    if year == 2011:
      listAllDaysParameters2011_BES.append(allValues)
    elif year == 2012:
      listAllDaysParameters2012_BES.append(allValues)
    elif year == 2013:
      listAllDaysParameters2013_BES.append(allValues)
    elif year == 2014:
      listAllDaysParameters2014_BES.append(allValues)
    elif year == 2015:
      listAllDaysParameters2015_BES.append(allValues)
    elif year == 2016:
      listAllDaysParameters2016_BES.append(allValues)
    else: print("This year is not provided by the dataset")

  elif type=="Zonal":
    if year == 2011:
      listAllDaysParameters2011_Zonal.append(allValues)
    elif year == 2012:
      listAllDaysParameters2012_Zonal.append(allValues)
    elif year == 2013:
      listAllDaysParameters2013_Zonal.append(allValues)
    elif year == 2014:
      listAllDaysParameters2014_Zonal.append(allValues)
    elif year == 2015:
      listAllDaysParameters2015_Zonal.append(allValues)
    elif year == 2016:
      listAllDaysParameters2016_Zonal.append(allValues)
    else: print("Sorry, this year is not provided by the dataset")

  else: print("Sorry, this type is not provided by the dataset")

关于此行代码的错误:

data.iloc[[x]["Date"]] == "1/9/"+str(year)+"12:00:00" 

如何修复此错误,因为我try 了堆栈溢出的多个解决方案,但都不起作用.

推荐答案

Here's how I modify that part of your code:

if data.loc[x, "Date"] == "1/" + month + "/" + str(year) + " 12:00:00":
    print("I am inside")
    # Rest of your code...

Python相关问答推荐

Python-Polars:如何用两个值的平均值填充NA?

Python:根据创建时间合并两个收件箱

使用Python从HTTP打印值

为什么使用SciPy中的Distance. cos函数比直接执行其Python代码更快?

如何获取Django REST框架中序列化器内部的外卡属性?

Plotly:如何更改Heatmap中彩色条的勾选文本

pandas DataFrame中类型转换混乱

在Windows上启动新Python项目的正确步骤顺序

带条件计算最小值

海运图:调整行和列标签

为什么符号没有按顺序添加?

如何在虚拟Python环境中运行Python程序?

如何在python polars中停止otherate(),当使用when()表达式时?

梯度下降:简化要素集的运行时间比原始要素集长

如何指定列数据类型

用渐近模计算含符号的矩阵乘法

在单次扫描中创建列表

如何使用两个关键函数来排序一个多索引框架?

在Python中使用yaml渲染(多行字符串)

具有相同图例 colored颜色 和标签的堆叠子图