Ваш опубликованный код (до вашего последнего редактирования) был неисправен, в нем было много неизвестного, что выдает ошибки, отличные от того, что вы опубликовали. Я исправил ваш код так:
dflist = [] # added this
for i,j in crispr.iteritems():
for x in range(len(j)):
test = j[x].isspace() # changed countchar to x
# countchar+=1 # removed this
if test == True:
dflist.append(0)
else:
dflist.append(1)
for i in crispr.itertuples():
for j in i:
for b in j: # this procudes your error
print(b)
Если вы осмотрите первый элемент j
, вы увидите его значение 0 - отсюда и ошибка. Вы не можете перебрать 0
.
Решение:
import pandas as pd
crispr = pd.DataFrame({
'Name': ['Bob', 'Jane', 'Alice'],
'Issue': ['Handling data', 'Could not read sample', 'No clue'],
'Comment': ['Need to revise data', 'sample preparation',
'could not find out where problem occurs']})
print(crispr)
outer_list = []
for i,j in crispr.iteritems():
dflist = []
for word in j:
wordlist = []
for char in word:
if char.isspace():
wordlist.append(0)
else:
wordlist.append(1)
dflist.append(wordlist)
outer_list.append(dflist)
print(outer_list)
Вывод (добавлены комментарии для ясности):
Comment Issue Name
0 Need to revise data Handling data Bob
1 sample preparation Could not read sample Jane
2 could not find out where problem occurs No clue Alice
# Comment
[[[1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]],
# Issue
[[1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 0, 1, 1, 1, 1]],
# Name
[[1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1, 1]]]
должен делать то, что вы хотите.