我有一个像这样的DataFrame
Classification Value_1 Value_2
churn 1.0 2.0
not_churn 2.0 3.0
not_churn 0.0 0.0
churn 0.0 1.0我知道在所有值都为0的情况下,分类应该是churn。然后,我需要删除所有值为0且分类为not_churn的所有行。我试过了:
df.drop((df['value_1'] == 0
& df['value_2'] == 0
& df['classification']== 'not_churn').index)
'TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]'发布于 2018-04-22 15:47:09
将boolean indexing与不等于!=的更改条件一起使用,并将&更改为| (或):
df = df[(df['Value_1'] != 0 ) | (df['Value_2'] != 0) | (df['Classification'] != 'not_churn')]
print (df)
Classification Value_1 Value_2
0 churn 1.0 2.0
1 not_churn 2.0 3.0
3 churn 0.0 1.0发布于 2018-04-22 15:53:03
如果您想根据您不想要的内容保留行,我会尝试:
df = df[~((df['value_1'] == 0) & (df['value_2'] == 0) & (df['classification'] == 'not churn'))]https://stackoverflow.com/questions/49963694
复制相似问题