![](/img/trans.png)
[英]Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby
[英]Pandas_select rows from a dataframe based on column values
我是 Python 的新手。 我有一個如下所示的數據框。 這是一個 CSV 文件。 我需要 select 所有包含頻率值 0.8 和 0.6 的行。 我編寫了如圖所示的代碼,但它拋出了一個錯誤。
df_new = df[df['頻率'] == 0.8 & df['頻率'] == 1.6 ]
下面是我收到的錯誤的最后一行。
“TypeError:無法使用 dtyped [float64] 數組和 [bool] 類型的標量執行 'rand_'”
我運行了下面的代碼
df_new = df[(df['頻率'] == 0.8) & (df['頻率'] == 1.6)]
它沒有顯示任何錯誤但沒有出現值。它只顯示列的名稱。請參閱 bwloe 圖像
由於&
和==
的優先級,您需要添加括號
df_new = df[(df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
在條件周圍添加圓括號
df_new = df[ (df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
你想要這個嗎?
df_new = df[df['Frequency'].isin([0.8,1.6])]
它沒有顯示答案原因和條件不匹配。 使用OR
代替AND
df_new = df[ (df['Frequency'] == 0.8) | (df['Frequency'] == 1.6) ]
您正在使用這就是為什么您得到一個空的dataframe 。 頻率不能同時為 0.8 和 0.6。 使用| 反而。
嘗試這個:
df = df[(df['Frequency'] == 0.8) | (df['Frequency'] == 0.6)]
或者
df = df[df["Frequency"].isin([0.6,0.8])]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.