KevsRobots Learning Platform
40% Percent Complete
By Kevin McAleer, 2 Minutes
Welcome to the lesson on Basic Operations with Data Frames in Pandas. Data Frames are central to data manipulation in Pandas, and knowing how to effectively perform various operations on them is key to unlocking their full potential. This lesson covers essential techniques like selection, filtering, sorting, and basic computations.
To select a single column, use the column label:
# Selecting a single column
selected_column = df['ColumnName']
To select multiple columns, use a list of column labels:
# Selecting multiple columns
selected_columns = df[['Column1', 'Column2']]
Rows can be selected by their position using iloc
or by index using loc
:
# Selecting rows by position
rows_by_position = df.iloc[0:5] # First five rows
# Selecting rows by index
rows_by_index = df.loc['IndexLabel']
You can filter data based on conditions:
# Filtering data
filtered_data = df[df['ColumnName'] > value]
Data in a Data Frame can be sorted by the values of one or more columns:
# Sorting data
sorted_data = df.sort_values(by='ColumnName')
Pandas allows for basic statistical computations:
# Basic computations
mean_value = df['ColumnName'].mean()
sum_value = df['ColumnName'].sum()
You can drop rows or columns from a Data Frame:
# Dropping rows
df_dropped_rows = df.drop([0, 1, 2])
# Dropping columns
df_dropped_columns = df.drop(['Column1', 'Column2'], axis=1)
Axis
Note that
axis=1
indicates that the operation should be performed on columns, whereasaxis=0
indicates that it should be performed on rows.
In this lesson, we have covered the basics of data selection, filtering, sorting, and performing 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.