Learn to code for data analysis
Learn to code for data analysis

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

Free course

Learn to code for data analysis

1.6 Bitwise operators

To build more complicated expressions involving column comparisons, there are two bitwise operators.

The & operator means ‘and’ and the | operator (vertical bar, not uppercase letter ‘i’) means ‘or’. So, for example the expression:

(df['Country'] >= 'Latvia') & (df['Country'] <= 'Sweden')

will evaluate to a series containing Boolean values where the values areTrue only if the equivalent rows in the dataframe contain the countries ‘Latvia’ to ‘Sweden’, inclusive. However, the following expression which uses | (or) rather than & (and):

(df['Country'] >= 'Latvia') | (df['Country'] <= 'Sweden')

will evaluate to True for all countries, because every country comes alphabetically after ‘Latvia’ (e.g. the ‘UK’) or before 'Sweden' (e.g. ‘Brazil’).

Note the round brackets around each comparison. Without them you will get an error.

The whole expression with multiple comparisons has to be put within df[…] to get a dataframe with only those rows that match the condition.

As a further example, using different columns, it is relatively easy to find the rows in df where 'Population (1000s)' is greater than 80000 and where 'TB deaths' are greater than 10000.

In []:

df[(df['Population (1000s)'] > 80000) & (df['TB deaths'] > 10000)]

Out []:

 CountryPopulation (1000s)TB deaths
141Russian Federation14283417000
190Viet Nam9168017000

These expressions can get long and complicated, making it easy to miss a crucial round or square bracket. In those cases it is best to break up the expression into small steps. The previous example could also be written as:

In []:

population = df['Population (1000s)']

deaths = df['TB deaths']

df[(population > 80000) & (deaths > 10000)]

Exercise 3 Bitwise operators

Complete Exercise 3 in the Exercise notebook 2.


Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371