赞
踩
您必须在其中插入带有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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。