0

I am at the beginning of learning pandas, I have data frame about cars and I want to return the models of car that are automatic and manufactured in the last two years. I am trying to write the code as below:

df.loc[((df[['year']].drop_duplicates().nlargest(2, 'year')) && (df.loc[df['Sparetype'] =='Automatic'])) ,'model']

But I get this error:

File "C:\Users\Hala\AppData\Local\Temp/ipykernel_3412/2229436407.py", line 1
    df.loc[((df[['year']].drop_duplicates().nlargest(2, 'year')) && (df.loc[df['Sparetype'] =='Automatic'])) ,'model']
                                                                  ^
SyntaxError: invalid syntax

When I replace && to & I get this error:

TypeError: unsupported operand type(s) for &: 'float' and 'int'
Neuron
  • 5,141
  • 5
  • 38
  • 59
  • 1
    Replace `&&` by `&` – ThePyGuy Sep 02 '21 at 16:12
  • Does this answer your question? [pandas: multiple conditions while indexing data frame - unexpected behavior](https://stackoverflow.com/questions/22591174/pandas-multiple-conditions-while-indexing-data-frame-unexpected-behavior) – rpanai Sep 02 '21 at 16:51
  • when i use & instead of && i get other error , this( TypeError: unsupported operand type(s) for &: 'float' and 'int') – Hala Abbas Sep 02 '21 at 18:30

1 Answers1

1

There is no double ampersand && in python, instead use and

Quixotic22
  • 2,894
  • 1
  • 6
  • 14