【问题标题】:df.drop not dropping rowdf.drop 不删除行
【发布时间】:2022-10-07 07:24:39
【问题描述】:

我有一个 for 循环,它遍历数据框并询问我们是否要删除每一行。无论我做什么,如果我说 Y,我都不能让行下降。

import pandas as pd
import random
from csv import writer
import csv

df1 = pd.read_csv(\'questions.csv\', usecols=[\'question_id\',\'question\'])
col1 = df1.question_id
col2 = df1.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,inplace=True)
        elif Check2 == \"Y\":
            Check4 = input(\"Please rewrite the question: \")
            df1.loc[index, \'question\'] = Check4

我的df是这样的: df1 =

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

谢谢你的帮助!

  • 代码没有任何问题,除了缩进为了如果声明,并且如果您对给定的数据框似乎完美地工作命名列 X 和 Y.您能否提供有关数据框的更多详细信息,例如列名?
  • 更新了在 Check3 之前删除缩进的问题,我的代码中没有缩进。我的列名是 [\'question_id\' ,\'question\']。
  • 它似乎工作得很好@Parker。我无法从您当前的解释中准确指出实际问题出在哪里。命名配置可能有问题,或者如果没有,请尝试在迭代之前重置索引。
  • 感谢您的答复!您是否认为我的数据框可能来自 CSV 文件?
  • 如果您使用过 pd.read_csv(),那应该不是问题。如果可能的话,您能否粘贴您尝试过的完整代码,即从读取数据框到删除行?

标签: python pandas dataframe row


【解决方案1】:

[编辑]我试过这个,它对我有用。不知道为什么它不适合你。也许给我们你的数据框(或它的样本)来测试它。

>>> df1 = pd.DataFrame({'col1':['-1', '-2', -3, -4], 'col2':['-2', '-6', 44, 66], 'col3':['15', 16, 17, 29]})
>>> 
>>> for index, row in df1.iterrows():
...     Check3 = input("Are you sure you want to Delete this question? (Y/N) ")
...     if Check3 == "Y":
...       df1.drop(index, inplace=True)
... 
Are you sure you want to Delete this question? (Y/N) Y
Are you sure you want to Delete this question? (Y/N) N
Are you sure you want to Delete this question? (Y/N) Y
Are you sure you want to Delete this question? (Y/N) N
>>> 
>>> df1
  col1 col2 col3
1   -2   -6   16
3   -4   66   29

【讨论】:

  • 抱歉 - 在粘贴问题时,我必须添加缩进。我的代码中没有缩进。即使我仍然无法让行下降。我的列标题只是 ['question_id', 'question']
【解决方案2】:

这段代码对我有用。
笔记:我正在为 python 3.8+ 使用“海象”运算符

df = pd.DataFrame({'Questions':[f"Question {i}" for i in range(1,6)],
                   'Text':["What is your number?", "What is your email?",
                           "What is your job title?", "What color is your car?",
                           "Do you have children?"]})
for indx, row in df.iterrows():
    print(row)
    print('\n')
    if (check3 := input("Are you sure you want to delete this row? ") == "Y"):
        df.drop(indx, inplace=True)
    print('\n')

输出:

Questions              Question 1
Text         What is your number?
Name: 0, dtype: object


Are you sure you want to delete this row? N


Questions             Question 2
Text         What is your email?
Name: 1, dtype: object


Are you sure you want to delete this row? Y


Questions                 Question 3
Text         What is your job title?
Name: 2, dtype: object


Are you sure you want to delete this row? N


Questions                 Question 4
Text         What color is your car?
Name: 3, dtype: object


Are you sure you want to delete this row? N


Questions               Question 5
Text         Do you have children?
Name: 4, dtype: object


Are you sure you want to delete this row? Y

还有,print(df)

    Questions                     Text
0  Question 1     What is your number?
2  Question 3  What is your job title?
3  Question 4  What color is your car?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多