Understanding the & operator in pandas

I am trying to utilize the & operator in my dataset with pandas.

This is for my first portfolio project with pandas. The lesson material that speaks to how to do this with the syntax instructions is found here: https://www.codecademy.com/paths/learn-python-for-data-science/tracks/intro-to-python-for-data-science-lpfds/modules/sorting-and-filtering-rows/lessons/sorting-and-filtering-rows/exercises/combining-booleans-with-and

what steps have I taken to solve the problem?
I looked at this stack overflow article and he converted his values to a float. He talks about series being passed in.

My question after reading this is, what is a series?

Python Pandas Series - GeeksforGeeks → according to geeks for geeks:

Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.).

I am thinking that I need to convert one of the column types so that the other can be grouped togather.

I run the code:

I have double checked my work and yet I am still getting this error. Can anyone inform me of where I went wrong?
My github repository is: GitHub - strikeouts27/jupyter-data_scientist_salary_projects: An analysis about how data scientist have been compensated in 2022

the error message I found was

ValueError: Unable to coerce list of <class 'pandas.core.series.Series'> to Series/DataFrame
data_analyst = df[(is_data_analyst) & (df['salary_in_usd'] < 60,000)]

However I get the following traceback error:

ValueError                                Traceback (most recent call last)
Cell In[150], line 1
----> 1 data_analyst = df[(is_data_analyst) & (df['salary_in_usd'] < 60,000)]

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/common.py:81, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     77             return NotImplemented
     79 other = item_from_zerodim(other)
---> 81 return method(self, other)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/arraylike.py:70, in OpsMixin.__and__(self, other)
     68 @unpack_zerodim_and_defer("__and__")
     69 def __and__(self, other):
---> 70     return self._logical_method(other, operator.and_)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/frame.py:7455, in DataFrame._arith_method(self, other, op)
   7452 axis: Literal[1] = 1  # only relevant for Series other case
   7453 other = ops.maybe_prepare_scalar_for_op(other, (self.shape[axis],))
-> 7455 self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
   7457 new_data = self._dispatch_frame_op(other, op, axis=axis)
   7458 return self._construct_result(new_data)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/__init__.py:302, in align_method_FRAME(left, right, axis, flex, level)
    299 elif is_list_like(right) and not isinstance(right, (ABCSeries, ABCDataFrame)):
    300     # GH 36702. Raise when attempting arithmetic with list of array-like.
    301     if any(is_array_like(el) for el in right):
--> 302         raise ValueError(
    303             f"Unable to coerce list of {type(right[0])} to Series/DataFrame"
    304         )
    305     # GH17901
    306     right = to_series(right)

ValueError: Unable to coerce list of <class 'pandas.core.series.Series'> to Series/DataFrame

Two things,
Did you try removing the comma in the salary?
ex:

and, the parens and brackets seem misplaced. Aren’t is_data_analyst and salary_in_usd column names?

data_analyst = df[(df['is_data_analyst']) & (df['salary_in_usd'] < 60000)]

‘salary_in_usd’ is a column.

is_data_analyst is a variable. However, when I look at the syntax in the lesson it says quotes. But maybe that is not the case for variables.

I questioned the comma in the dollar amount. Shouldn’t it just be 60000, rather than 60,000?

Did you try:

data_analyst = df[(df['is_data_analyst']) & (df['salary_in_usd'] < 60000)]

result = df[df[“job_title”] == “Data Analyst”) & (df[‘salary_in_usd’] < 60000)]

ERROR MESSAGE:

result = df[df["job_title"] == "Data Analyst") & (df['salary_in_usd'] < 60000)]

SyntaxError: closing parenthesis ‘)’ does not match opening parenthesis ‘[’

Yea, there’s a missed parens. It’s hard to see. It’s on the first thing you’re comparing, job_title.

result = df[(df["job_title"] == "Data Analyst") & (df['salary_in_usd'] < 60000)]

Checking Syntax requirements to see if I can make this work

Thank you lisalisaj after checking the codecademy syntax and fixing the comma and the & sign it seems to work.

1 Like