Confused
Confused

Reputation: 353

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

enter image description here

enter image description here

Upvotes: 1

Views: 17131

Answers (5)

Muhammad Safwan
Muhammad Safwan

Reputation: 1024

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

Upvotes: 2

Muhammad Safwan
Muhammad Safwan

Reputation: 1024

add round bracket around the conditions

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

Upvotes: 2

Hamza usman ghani
Hamza usman ghani

Reputation: 2243

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

Upvotes: 2

Nk03
Nk03

Reputation: 14949

Do you want this?

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

Upvotes: 2

Ynjxsjmh
Ynjxsjmh

Reputation: 29982

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

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

Upvotes: 1

Related Questions