我正在將CSV導入熊貓數據框。當我這樣做時,我將索引列設置爲0,即列出的索引(0到10)。我收到錯誤密鑰錯誤:標籤[1]不在[索引]中。pd.read_csv中的字符串行索引導致錯誤「標籤[1]不在[index]中」
我檢查了數據多次,以確保第一列是數字列表。有關我如何解決這個問題的任何提示?
from __future__ import division
import pandas as pd
import random
import math
#USER VARIABLES
#GAME VARIABLES
Passengers = 500
data = pd.read_csv("Problem2/data.csv", index_col=0)
print(data)
obs = len(data)
data["A"] = 0
data["B"] = 0
data["U"] = 0
for row in range(1,obs+1, 1):
A = 0
B = 0
U = 0
for i in range(1, Passengers + 1, 1):
if data.loc[row, i] == "A":
A += 1
elif data.loc[row, i] == "B":
B += 1
else:
U += 1
data.loc[row, "A"] = A
data.loc[row, "B"] = B
data.loc[row, "U"] = U
ServiceLevels = range(170, 210,1)
for level in ServiceLevels:
print(str(level) + " " + str(len(data[((data.A <= level))])/obs))
數據集= https://github.com/deacons2016/SimulationModels/blob/master/Exam1/Problem2/data.csv
這個CSV文件有503列,爲14.5 MB和如此之大,github上拒絕顯示預覽(!)。請將您的示例減少到未來需要重新處理問題的最小大小。 – smci
重複的[如何讀取索引數據作爲字符串與pandas.read \ _csv()?](https://stackoverflow.com/questions/35058435/how-to-read-index-data-as-string-with- pandas-read-csv)這個問題已經被問過很多次了。 – smci