1

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

enter image description here

enter image description here

1
  • 2
    try, df[(df['Frequency'] == 0.8) & (df['Frequency'] == 1.6)] Commented Apr 30, 2021 at 10:06

5 Answers 5

2

add round bracket around the conditions

df_new = df[ (df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
Sign up to request clarification or add additional context in comments.

2 Comments

I run this code.it is not showing any error . But o/p is not proper. Column names are coming but not the contents. I updated my question please see.
use OR instead of AND
2

Do you want this?

df_new = df[df['Frequency'].isin([0.8,1.6])] 

Comments

2

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])]

2 Comments

if you don't mind could you please tell me what is "isin" means?
isin() will match a value with list of values you provided. Lets say you want to compare frequency with 0.6, 0.8,1,2,3.. So writing individual condition will be a mess. You can simply provide all values to a list and use isin()
2

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) ]

Comments

1

You need add bracket because of the priority of & and ==

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

1 Comment

I run this code.it is not showing any error . But o/p is not proper. Column names are coming but not the contents. I updated my question please see.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.