Search code examples
pythonpandasdataframeloopsdrop

Pandas Dataframe dropping and/or replacing values


I have a Pandas dataframe full of questions. My program goes through each question and asks the user if the question is okay or if it needs to be changed or dropped from the matrix - I haven't been able to get either to take.

df1 = pd.read_csv('questions.csv', usecols=['question_id','question'])
for index, row in df1.iterrows():
 print(row['question'])
 Check1 = input("Is the following question correct? (Y/N): ")
 if Check1 == "Y":
    continue
 elif Check1 == "N":
    Check2 = input("Is this question Needed? (Y/N):")
     if Check2 == "N":
         Check3 = input("Are you sure you want to Delete this question? (Y/N)")
         if Check3 == "Y":
             df1.drop(df1.index[index], inplace=True)
     elif Check2 == "Y":
         Check4 = input("Please rewrite the question: ")
         df1['question'] = df1.replace(['question'],'Check4')

Looking for help with df1.drop - if I need to remove the whole row. or df1.replace if I need to just replace the one cell value.

Edit: Here is my data set - the numbers are identifiers:

question_id,question
12,What is your number?
10,What is your email?
6,What is your Job title?
30,What color is your car?]

Solution

  • To drop the row/question chosen by the user, you can use pandas.DataFrame.drop and pass the index of the row as a label. And to replace the value of a specific cell/question chosen by the user to be rephrased, you can use pandas.DataFrame.loc.

    Your code would look like this :

    for index, row in df1.iterrows():
        print(row['question'])
        Check1 = input("Is the following question correct? (Y/N): ")
        if Check1 == "Y":
            continue
        elif Check1 == "N":
            Check2 = input("Is this question Needed? (Y/N):")
            if Check2 == "N":
                Check3 = input("Are you sure you want to Delete this question? (Y/N)")
                if Check3 == "Y":
                     df1.drop(index, inplace=True)
            elif Check2 == "Y":
                Check4 = input("Please rewrite the question: ")
                df1.loc[index, 'question'] = Check4