-1

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
5
  • maybe you should use .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 without for-loop but using .apply(). OR you should first check if idx >= 0. OR you should skip first row (because it doesn't have previous row) and use data['text'][1:] Commented Sep 24, 2022 at 22:31
  • c is text from cell, not dataframe. You should use c = data['text'][idx]. Other method is to always keep previous = c and late you can use c = previous Commented Sep 24, 2022 at 22:36
  • Maybe first use print() (and print(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. Commented Sep 24, 2022 at 22:38
  • BTW: sometimes you may have dataframe which doesn't use consecutive numbers as indexes and it can be safrer to use for index, value in data['text'].iterrows() and remeber previous_index = index and in next loop use data['text'][previous_index]. And before loop you can set previous_index = None and in loo check if previous_index is not None Commented Sep 24, 2022 at 22:43
  • you probably want a while loop as opposed to for ... Commented Sep 24, 2022 at 23:50

1 Answer 1

1

If you want to go back to previous row then you should use while-loop and manually change index += 1 or index -= 1 and get text = data['text'][index] or row = data.loc[index]

But all this will work only if indexes use consecutive numbers.

import pandas as pd

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['label'] = ''
print(data)

index = 0

while index < len(data):
    row = data.loc[index]
        
    print("Enter labels for the text or enter 2 to go back to previous:")
    print()
    print('index:', index)
    print('text :', row['text'])
    print('label:', row['label'])
    print()
    
    label = input(": ")
    
    if label == '2':
        index -= 1  # go to previous
        if index < 0:
            print('There is no previous row')    
            index = 0
    else:
        #row['label'] = label
        data.loc[index, 'label'] = label
        index += 1  # go to next
    
print(data)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.