[英]Iterate over columns in a row in pandas
I have a csv file with following headers我有一个带有以下标题的 csv 文件
question_no,question,A,B,C,D
where A,B,C,D are options for a question.其中 A,B,C,D 是问题的选项。 The number of options for a question can vary from file to file(for eg. 4 - A,B,C,D 6 - A,B,C,D,E,F).
问题的选项数量可能因文件而异(例如 4 - A,B,C,D 6 - A,B,C,D,E,F)。 I am trying to get the values of options in the row using the following code.
我正在尝试使用以下代码获取行中选项的值。
data = pd.read_csv(request.FILES['myfile'])
optioncodes = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
col_nos = len(data.columns)
opt_lmt = col_nos - 2
for (idx, row) in data.iterrows():
print(row.question_no)
for j in range(opt_lmt):
print(row.optioncodes[j])
but I am getting the error但我得到了错误
'Series' object has no attribute 'optioncodes'
How can I achieve this?我怎样才能做到这一点?
The dot accessor ( df.col_name
or serie.index_value
) is only a shortcut for the named element accessor ( df['col_name']
or serie['index_value']
).点访问器(
df.col_name
或serie.index_value
)只是命名元素访问器( df['col_name']
或serie['index_value']
)的快捷方式。 And it is only valid at 2 conditions:它仅在 2 个条件下有效:
What you want here is just:你想要的只是:
...
for j in range(opt_lmt):
print(row[optioncodes[j]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.