[英]Pandas_select rows from a dataframe based on column values
I am new to Python.我是 Python 的新手。 I have a data frame as shown below.
我有一个如下所示的数据框。 This is a CSV file.
这是一个 CSV 文件。 I need to select all rows which contain Frequency values 0.8 and 0.6.
我需要 select 所有包含频率值 0.8 和 0.6 的行。 I wrote the codes as shown but it is throwing an error.
我编写了如图所示的代码,但它抛出了一个错误。
df_new = df[df['Frequency'] == 0.8 & df['Frequency'] == 1.6 ] df_new = df[df['频率'] == 0.8 & df['频率'] == 1.6 ]
Below is the last line from the error I received.下面是我收到的错误的最后一行。
"TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]" “TypeError:无法使用 dtyped [float64] 数组和 [bool] 类型的标量执行 'rand_'”
I ran the below code我运行了下面的代码
df_new = df[(df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ] df_new = df[(df['频率'] == 0.8) & (df['频率'] == 1.6)]
It is nt showing any error but values are not coming.it is showing only the name of columns.Please see the bwloe image它没有显示任何错误但没有出现值。它只显示列的名称。请参阅 bwloe 图像
You need add bracket because of the priority of &
and ==
由于
&
和==
的优先级,您需要添加括号
df_new = df[(df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
add round bracket around the conditions在条件周围添加圆括号
df_new = df[ (df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
Do you want this?你想要这个吗?
df_new = df[df['Frequency'].isin([0.8,1.6])]
Its not showing an answer causer and condition is not matching.它没有显示答案原因和条件不匹配。 use
OR
instead of AND
使用
OR
代替AND
df_new = df[ (df['Frequency'] == 0.8) | (df['Frequency'] == 1.6) ]
You are using & thats why you are getting an empty dataframe.您正在使用这就是为什么您得到一个空的dataframe 。 Frequency can not be 0.8 and 0.6 at the same time.
频率不能同时为 0.8 和 0.6。 Use |
使用| instead.
反而。
Try this:尝试这个:
df = df[(df['Frequency'] == 0.8) | (df['Frequency'] == 0.6)]
OR或者
df = df[df["Frequency"].isin([0.6,0.8])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.