How to Use Pandas for Data Manipulation: A Practical Tutorial

Pandas is Python’s go-to library for cleaning, transforming, and analyzing tabular data. This tutorial covers the essential operations you’ll use every day.

Start by importing it and loading your dataset. Pandas reads CSV, Excel, JSON, and SQL sources out of the box with pd.read_csv() and friends.

Article illustration

1. Inspect Your Data First

Before changing anything, understand what you have. Run df.head(), df.info(), and df.describe() to check structure, data types, and summary statistics. This surfaces missing values early.

2. Select and Filter Rows

Use df['column'] for a Series and df[['a','b']] for multiple columns. Filtering works like this:

  • df[df['age'] > 30] — condition-based filter
  • df.loc[0:5, 'name'] — label-based selection
  • df.iloc[0:5, 0:2] — position-based selection

3. Clean and Transform

Handle gaps with df.dropna() or df.fillna(0). Create fields with df['total'] = df['price'] * df['qty'], and rename columns using df.rename(columns={...}).

4. Group and Aggregate

Summarize categories with df.groupby('region')['sales'].sum(). Chain .agg() to compute several statistics at once.

Conclusion

Loading, inspecting, filtering, and grouping cover most day-to-day pandas work. Practice on real datasets and the syntax quickly becomes second nature.

sarah antaboga
Author: sarah antaboga

Leave a Reply

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