I have a dataframe similar to the below:
data = {'part': ['c', 'c', 'c', 'p', 'p',
'p', 'p', 'p'], 'index': [0,1,2,3,4,5,6,7], 'text':
['a','b','c', 'd', 'e', 'f', 'g', 'h'], 'class': [[1,0,0],
[0,1,0], [1,1,0], None , None , None , None , None]}
data = pd.DataFrame(data)
data
I am trying to iterate through this data frame to display the strings of the column "text" to users so that they can name these strings. I also want to provide users the chance to return to previously presented strings in case they change their mind and need to rename previous strings. How can I achieve this goal? I am trying the below code:
for n, c in enumerate(data['text']):
a = input(f"Enter labels for the text or enter 2 to go back to
previous: \n\n{c}\n\n: ")
if a == '2':
idx = n - 1
c = c[idx]
IndexError Traceback (most recent
call last)
<ipython-input-59-ca3c4aa6f370> in <module>
3 if a == '2':
4 idx = n - 1
----> 5 c = c[idx]
6
7
IndexError: string index out of range
.shift(1)to create new column with value from previous row - and you will have both values in the same row - and then you can try to do it withoutfor-loop but using.apply(). OR you should first checkif idx >= 0. OR you should skip first row (because it doesn't have previous row) and usedata['text'][1:]cis text from cell, not dataframe. You should usec = data['text'][idx]. Other method is to always keepprevious = cand late you can usec = previousprint()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debuging"and it helps to see what code is really doing.for index, value in data['text'].iterrows()and remeberprevious_index = indexand in next loop usedata['text'][previous_index]. And before loop you can setprevious_index = Noneand in loo checkif previous_index is not None