KevsRobots Learning Platform
By Kevin McAleer, 3 Minutes

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.
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']]
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.
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')]
# Lowest battery first
by_battery = df.sort_values(by='battery')
# Highest battery first
by_battery_desc = df.sort_values(by='battery', ascending=False)
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.
# 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=1means “columns” andaxis=0means “rows”. Usingcolumns=is easier to read.
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?
iloc[0] is always the first row, while loc[0] is the row labelled 0.In this lesson, we have covered data selection, filtering, sorting, and basic computations in Pandas. These operations are fundamental to data manipulation and analysis.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments