简体   繁体   English

Pandas_select 基于列值从 dataframe 中选择行

[英]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.

相关问题 使用 Groupby 根据 Pandas 中列中的值从 DataFrame 中选择 CONSECUTIVE 行 - Select CONSECUTIVE rows from a DataFrame based on values in a column in Pandas with Groupby 根据熊猫列中的字符串值从DataFrame中选择行 - Select rows from a DataFrame based on string values in a column in pandas 根据熊猫列中值的最后一个字符从DataFrame中选择行 - Select rows from a DataFrame based on last characters of values in a column in pandas 基于列值的 DataFrame 中的 Pandas select 行? - Pandas select rows from a DataFrame based on column values? 基于列值的 DataFrame 中的 select 行? - select rows from a DataFrame based on column values? Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 根据熊猫中一列中的逻辑测试从DataFrame中选择行 - Select rows from a DataFrame based on logical test in a column in pandas 在pandas的列中选择基于True或False的DataFrame中的行 - Select rows from a DataFrame based on True or False in a column in pandas 如何根据从另一列之间选择熊猫数据框中的行 - how to select rows in pandas dataframe based on between from another column Pandas - 来自 dataframe 的 Select 行基于列中的列表 - Pandas - Select rows from a dataframe based on list in a column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM