如何根据多种条件过滤DF

时间:2020-10-18 11:27:12

标签: python pandas

我有一个要使用多种条件过滤的df

remove_outliers[remove_outliers['outlier_residual'] > (Q3 + 1.5 * IQR) and remove_outliers['season'] =='Autumn']

尝试此操作时出现以下错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-304-141eedb8a594> in <module>
----> 1 remove_outliers[remove_outliers['outlier_residual'] > (Q3 + 1.5 * IQR) and remove_outliers['season'] =='Autumn']

~\AppData\Roaming\Python\Python37\site-packages\pandas\core\generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

什么是正确的方法? 感谢任何帮助或建议

2 个答案:

答案 0 :(得分:1)

remove_outliers.loc[(remove_outliers['outlier_residual'] > (Q3 + 1.5 * IQR)) & (remove_outliers['season'] =='Autumn')]

并且他们无需将 .loc 嵌套在 .loc

答案 1 :(得分:0)

我想你错过了一对括号。让我知道它现在是否有效:

remove_outliers.loc[(remove_outliers.loc[:,'outlier_residual'] > (Q3 + 1.5 * IQR)) & remove_outliers.loc[:,'season'] =='Autumn'),:]

PS我已将.loc用于良好实践目的

相关问题