我正在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 了堆栈溢出的多个解决方案,但都不起作用.