2015-11-08 26 views
2

我正在將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

+0

這個CSV文件有503列,爲14.5 MB和如此之大,github上拒絕顯示預覽(!)。請將您的示例減少到未來需要重新處理問題的最小大小。 – smci

+0

重複的[如何讀取索引數據作爲字符串與pandas.read \ _csv()?](https://stackoverflow.com/questions/35058435/how-to-read-index-data-as-string-with- pandas-read-csv)這個問題已經被問過很多次了。 – smci

回答

4

你在你與str投列。

In[60]: data = pd.read_csv(r'/Users/Desktop/data.csv', sep = ',', index_col = [0]) 

In[61]: obs = len(data) 

In[62]: data["A"] = 0 
     data["B"] = 0 
     data["U"] = 0 

In[63]: Passengers = 500 

In[64]: for row in range(1,obs+1): 
      print row 
      A = 0 
      B = 0 
      U = 0 
      for i in range(1, Passengers + 1, 1): 
       if data.loc[row, str(i)] == "A": 
        A += 1 
       elif data.loc[row, str(i)] == "B": 
        B += 1 
       else: 
        U += 1 
      data.loc[row, "A"] = A 
      data.loc[row, "B"] = B 
      data.loc[row, "U"] = U 
1 
. 
. 
10 

的最短辦法做到這一點:

data = pd.read_csv(r'/Users/Desktop/data.csv', sep = ',', index_col = [0]) 

cols = data.columns 
data['A'] = (data[cols] == 'A').astype(int).sum(axis=1) 
data['B'] = (data[cols] == 'B').astype(int).sum(axis=1) 
data['U'] = (data[cols] == 'U').astype(int).sum(axis=1) 
+0

謝謝!我一直堅持這一點。 –

+0

@JeremyLewallen:我添加了最短的方式來做到這一點在4行:) –

+0

謝謝。我不知道這是可能的。 :) –

相關問題