简体   繁体   中英

Pandas_select rows from a dataframe based on column values

I am new to Python. I have a data frame as shown below. This is a CSV file. I need to select all rows which contain Frequency values 0.8 and 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 ]

Below is the last line from the error I received.

"TypeError: Cannot perform 'rand_' with a dtyped [float64] array and scalar of type [bool]"

I ran the below code

df_new = df[(df['Frequency'] == 0.8) & (df['Frequency'] == 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

在此处输入图像描述

在此处输入图像描述

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

df_new = df[ (df['Frequency'] == 0.8) | (df['Frequency'] == 1.6) ]

You are using & thats why you are getting an empty dataframe. Frequency can not be 0.8 and 0.6 at the same time. Use | instead.

Try this:

df = df[(df['Frequency'] == 0.8) | (df['Frequency'] == 0.6)]

OR

df = df[df["Frequency"].isin([0.6,0.8])]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM