Reputation: 353
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
Upvotes: 1
Views: 17131
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
Reputation: 1024
add round bracket around the conditions
df_new = df[ (df['Frequency'] == 0.8) & (df['Frequency'] == 1.6) ]
Upvotes: 2
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
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