Basic Operations

Master the fundamental operations with Data Frames in Pandas, including data selection, filtering, sorting, and basic computations.

By Kevin McAleer,    3 Minutes


Cover photo

Overview

Data Frames are central to data manipulation in Pandas, and knowing how to select, filter, sort and summarise them is key to unlocking their power. In this lesson we’ll use a small table of robot sensor readings, which we’ll keep using for the next few lessons.

import pandas as pd

df = pd.DataFrame({
    'robot':   ['BurgerBot', 'SMARS', 'BurgerBot', 'SMARS', 'Quad', 'Quad'],
    'sensor':  ['distance', 'distance', 'line', 'distance', 'imu', 'distance'],
    'reading': [23.5, 41.0, 1.0, 12.2, None, 30.1],
    'battery': [87, 64, 85, 60, 92, 90],
})
print(df)

Data Selection

Selecting Columns

To select a single column, use the column name. You get a Series back:

batteries = df['battery']

To select multiple columns, use a list of column names. You get a Data Frame back:

subset = df[['robot', 'battery']]

Selecting Rows

Rows can be selected by their position using iloc, or by their index label using loc:

# By position: the first three rows
first_three = df.iloc[0:3]

# By label: the row whose index is 2
row = df.loc[2]

Our index is just the numbers 0 to 5, so loc and iloc look similar here. They behave differently once the index has other labels, such as names or dates.


Filtering Data

Put a condition inside the square brackets to keep only the rows where it’s true:

# Robots with more than 80% battery
charged = df[df['battery'] > 80]

# Combine conditions with & (and) and | (or), with brackets around each one
smars_distance = df[(df['robot'] == 'SMARS') & (df['sensor'] == 'distance')]

Sorting Data

# Lowest battery first
by_battery = df.sort_values(by='battery')

# Highest battery first
by_battery_desc = df.sort_values(by='battery', ascending=False)

Basic Computations

print(df['battery'].mean())   # average battery level
print(df['battery'].sum())
print(df['battery'].max())

Pandas skips missing values in calculations like mean(), so the empty reading for the Quad doesn’t cause an error.


Dropping rows and columns

# Drop rows by index label
fewer_rows = df.drop([0, 1])

# Drop a column
no_sensor = df.drop(columns=['sensor'])

drop() returns a new Data Frame and leaves df unchanged. Assign the result back (df = df.drop(...)) to keep the change.

Axis

You’ll also see df.drop(['sensor'], axis=1). axis=1 means “columns” and axis=0 means “rows”. Using columns= is easier to read.


Try it yourself

Find all the readings from the distance sensor, sorted from nearest to furthest.

Quick quiz 1 of 2

What does df[df['battery'] > 80] return?

Quick quiz 2 of 2

What’s the difference between iloc and loc?


Summary

In this lesson, we have covered data selection, filtering, sorting, and basic computations in Pandas. These operations are fundamental to data manipulation and analysis.


< Previous Next >

You can use the arrows  ← → on your keyboard to navigate between lessons.


What are you looking for?
Watch Videos Get Ideas Learn Something Read a Review Read the Blog Search
... Z Z z