当前位置:   article > 正文

python导入csv报错_python-pd.read_csv中的字符串行索引导致错误“...

pd.read_csv导入数据行索引出现错误

您必须在其中插入带有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)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/175861
推荐阅读
相关标签
  

闽ICP备14008679号