Conditional Row Removal in Pandas DataFrames
Encountering the "KeyError: u'no item named False'" error when attempting to remove rows from a pandas DataFrame using the expression "df[(len(df['column name']) < 2)]" indicates an incorrect approach.
To directly address the issue of deleting rows based on a conditional expression, there are several methods available in pandas. One effective technique involves employing the drop() method:
df = df.drop(some_labels) df = df.drop(df[<boolean condition>].index)</p> <p><strong>Example:</strong></p> <p>Consider a DataFrame with a 'score' column. To remove all rows where the score is below 50:</p> <pre class="brush:php;toolbar:false">df = df.drop(df[df.score < 50].index)
For in-place deletion:
df.drop(df[df.score < 50].index, inplace=True)
Multiple Conditions:
Using Boolean indexing, it is possible to combine multiple conditions for row removal. For instance, to delete rows where 'score' is both below 50 and above 20:
df = df.drop(df[(df.score < 50) & (df.score > 20)].index)
By applying these conditional removal methods, it is straightforward to remove rows from pandas DataFrames based on specified criteria.
以上是如何根據條件表達式有效地從 Pandas DataFrame 中刪除行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!