首頁  >  文章  >  後端開發  >  如何根據條件表達式從 Pandas DataFrame 中刪除行?

如何根據條件表達式從 Pandas DataFrame 中刪除行?

DDD
DDD原創
2024-11-13 15:23:02569瀏覽

How to Delete Rows from a Pandas DataFrame Based on a Conditional Expression?

Removing Rows from Pandas DataFrame Based on Conditional Expression

Your approach of using df[(len(df['column name']) < 2)] to delete rows where the string length exceeds 2 is incorrect. This results in a KeyError because the expression evaluates to a boolean DataFrame with keys True and False, rather than row indices.

To delete rows based on a conditional expression, you can utilize the drop method. Here's how it works:

Using drop() to Delete Rows

df = df.drop(df[df['column name'].str.len() > 2].index)</p>
<p>In this example, df['column name'].str.len() > 2 creates a boolean DataFrame indicating rows with string lengths greater than 2. The index attribute of this DataFrame retrieves the indices of those rows, which are then passed to drop().</p>
<p><strong>Alternative Syntax</strong></p>
<pre class="brush:php;toolbar:false">df = df.drop(df[(df['column name'].str.len() > 2)].index)

This syntax provides a clearer separation between the boolean DataFrame and index extraction.

Multiple Conditions

Boolean indexing allows you to combine conditions using logical operators. For instance, to remove rows where the string length exceeds 2 and the score is less than 50:

df = df.drop(df[(df['column name'].str.len() > 2) & (df['score'] < 50)].index)

以上是如何根據條件表達式從 Pandas DataFrame 中刪除行?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn