Python pd.read_csv 中的字符串行索引导致错误“标签 [1] 不在 [索引] 中”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33589691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:36:46  来源:igfitidea点击:

String row-index in pd.read_csv causes error "The label [1] is not in the [index]"

pythonpandascsvindexing

提问by Rupert

I am importing a CSV into a pandas dataframe. When I am do this, I am setting the index column to 0, which is the Index listed (0 to 10). I am getting the error Key Error: the label [1] is not in the [index].

我正在将 CSV 导入到 Pandas 数据框中。当我这样做时,我将索引列设置为 0,即列出的索引(0 到 10)。我收到错误密钥错误:标签 [1] 不在 [索引] 中。

I've checked the data multiple times to make sure that the first column is the list of numbers. Any hints on how I can fix this?

我已经多次检查数据以确保第一列是数字列表。关于如何解决这个问题的任何提示?

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))

Dataset = https://github.com/deacons2016/SimulationModels/blob/master/Exam1/Problem2/data.csv

数据集 = https://github.com/deacons2016/SimulationModels/blob/master/Exam1/Problem2/data.csv

采纳答案by Alexis G

You have to cast columns with strin your for.

你必须str在你的 for 中投射列。

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

A shortest way to do that :

一个最短的方法来做到这一点:

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)