Codeskill

Learn to code, step by step

Data Analysis Basics with Python’s Pandas Library

Data analysis basics with Pandas – Series, DataFrames, and common operations on tabular data.

What is Pandas?

Pandas is an open-source library for working with tables and time series in Python. It handles the messy prep work – loading, cleaning, filtering, grouping – before you analyse or plot anything.

Setting up Pandas

pip install pandas

Series and DataFrame

Pandas has two main structures:

  • A Series is a one-dimensional labelled array – one column of data.
  • A DataFrame is a two-dimensional table – rows and columns, like a spreadsheet.

Creating a DataFrame

Build one from a dictionary, a list, or a file such as CSV:

import pandas as pd

data = {
    'Name': ['John', 'Anna', 'Peter', 'Linda'],
    'Age': [28, 34, 29, 32],
    'City': ['New York', 'Paris', 'Berlin', 'London']
}

df = pd.DataFrame(data)
print(df)

Basic DataFrame operations

Viewing data – peek at the top and bottom rows:

  print(df.head())  # First 5 rows
  print(df.tail())  # Last 5 rows

Descriptive statistics – a quick summary:

  print(df.describe())

Selecting data – pick a column or slice rows:

  print(df['Name'])  # Prints the 'Name' column
  print(df[0:2])    # Prints first two rows

Filtering data – keep rows that match a condition:

  print(df[df.Age > 30])  # Selects people older than 30

Reading and writing data

Pandas reads and writes CSV, Excel, JSON, HTML, and more.

Reading a CSV file:

  df = pd.read_csv('filename.csv')

Writing to a CSV file:

  df.to_csv('new_filename.csv')

Handling missing data

Real datasets have gaps. Pandas gives you options:

# Drop rows with missing values
df.dropna()

# Fill missing values
df.fillna(value=0)

Grouping data

Split rows into groups and run a function on each:

grouped = df.groupby('City')
print(grouped.mean())

Pivot tables

Summarise data by row and column labels:

table = pd.pivot_table(df, values='Age', index=['City'], columns=['Name'])
print(table)

Time series analysis

Pandas started in finance, so it handles dates and time series well:

ts = pd.date_range('2020-01-01', periods=6, freq='D')
df = pd.DataFrame(np.random.randn(6, 4), index=ts, columns=list('ABCD'))
print(df)

Visualisation

Pandas plugs into Matplotlib for quick plots:

import matplotlib.pyplot as plt

df.plot()
plt.show()

Advanced operations

Once the basics feel comfortable, look at merging DataFrames, text operations, and faster filtering with eval() and query().

Load a CSV, run head() and describe(), filter a column, save the result. Repeat on different datasets until the patterns stick.

PreviousPython and APIs: Interacting with Web Services