簡體   English   中英

當特定列包含向我發出信號應刪除該行的值時,如何刪除Pandas數據框中的行?

[英]How do I delete a row in Pandas dataframe when a specific column contains a value that signals to me that the row should be deleted?

每個人都非常簡單的問題,但是幾乎不可能在官方文檔中找到基本問題的答案。

我在Pandas中有一個具有行和列的dataframe對象。

名為“ CBSM”的列之一包含布爾值。 我需要從數據框CBSM列的值=“ Y”刪除所有行。

我看到有一個名為dataframe.drop()的方法

Label,Axis和Level是drop()方法采用的3個參數。我不知道提供這些參數的值是什么,以上述方式完成刪除行的需求。 我感覺drop()方法不是執行我想要的正確方法。

請指教,謝謝。

此方法稱為布爾索引

您可以嘗試locstr.contains

df.loc[~df['CBSM'].str.contains('Y')] 

樣品:

print df
   A CBSM  L
0  1    Y  4
1  1    N  6
2  2    N  3

print df['CBSM'].str.contains('Y')
0     True
1    False
2    False
Name: CBSM, dtype: bool

#inverted boolean serie
print ~df['CBSM'].str.contains('Y')
0    False
1     True
2     True
Name: CBSM, dtype: bool

print df.loc[~df['CBSM'].str.contains('Y')] 
   A CBSM  L
1  1    N  6
2  2    N  3

要么:

print df.loc[~(df['CBSM'] == 'Y')] 
   A CBSM  L
1  1    N  6
2  2    N  3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM