尝试迭代列并删除pandas dataframe中具有特定字符串值的行

时间:2016-05-19 20:04:09

标签: python pandas dataframe

我试图找到一种方法来遍历DataFrame的每一列并检查具有特定字符串的行,并将它们删除为机器学习练习的预处理步骤。手动执行此操作的代码如下:

train = train[train.Native_Country != ' ?']
train = train[train.Race != ' ?']
train = train[train.Work_Class != ' ?']
train = train[train.Occupation != ' ?']
train = train[train.Relationship != ' ?']
train = train[train.Sex != ' ?']
train = train[train.Work_Class != ' ?']
train = train[train.Occupation != ' ?']
train = train[train.Education != ' ?']

必须有一种更简单的方法来省略或删除每列的这些特定行,我觉得我正在过度思考如何为loop-if循环执行此操作。

1 个答案:

答案 0 :(得分:0)

在EdChum回答之后,尝试这样的事情:

 columnsyouwant = ['Native_Country', 'Race', 'Work_Class'....]

 for col in columnsyouwant:
    train[col] = train[col].replace(' ?', None)

 train.dropna(inplace=True) #default is how='any' in axis=0 (rows)