How to Use Pandas for Data Manipulation: A Practical Tutorial

Pandas is a powerful Python library for data manipulation and analysis. Whether you’re cleaning messy datasets, transforming columns, or performing group operations, Pandas provides intuitive tools that make these tasks efficient. This tutorial covers the essential operations you need to get started with real-world data wrangling.

Loading and Inspecting Your Data

First, import Pandas and load a CSV file using read_csv(). Use head() to preview the first few rows and info() to understand column types and missing values.

  • import pandas as pd
  • df = pd.read_csv('data.csv')
  • df.head() — see first 5 rows
  • df.info() — check data types and nulls

Article illustration

Selecting and Filtering Rows or Columns

Use bracket notation or loc / iloc for precise selection. To filter rows by a condition, use boolean indexing.

  • Select a column: df['column_name']
  • Select rows where value > 50: df[df['column'] > 50]
  • Select specific rows and columns with loc: df.loc[0:5, ['col1','col2']]

Cleaning and Transforming Data

Handle missing values with fillna() or dropna(). Rename columns using rename() and apply functions across rows or columns with apply().

  • df.fillna(df.mean()) — fill NaN with column mean
  • df.rename(columns={'old':'new'}, inplace=True)
  • df['new_col'] = df['col'].apply(lambda x: x * 2)

Grouping and Aggregating Data

Group data by a categorical column and compute summary statistics using groupby() with agg().

  • df.groupby('category')['value'].mean()
  • Multiple aggregations: df.groupby('cat').agg(['sum','mean','count'])

Mastering these core pandas operations will allow you to quickly clean, filter, and summarize any tabular dataset. Practice on real data to build confidence and speed up your data manipulation workflow.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *