Pandas Fundamentals
What is pandas?
pandas is Python's primary data analysis library. It provides two core data structures: the Series (a single column) and the DataFrame (a table of rows and columns). The DataFrame is conceptually similar to an Excel spreadsheet or a SQL table, but with the full power of Python for transformation and analysis.
import pandas as pd
import numpy as np
Creating DataFrames
From a dictionary
data = {
'name': ['Amara', 'Emeka', 'Chisom', 'Fatima'],
'country': ['Nigeria', 'Ghana', 'Nigeria', 'Kenya'],
'revenue': [1200, 450, 870, 320],
'orders': [5, 2, 4, 1]
}
df = pd.DataFrame(data)
From a CSV file
df = pd.read_csv('sales_data.csv')
# With options
df = pd.read_csv(
'sales_data.csv',
parse_dates=['order_date'], # automatically parse date columns
index_col='order_id' # use order_id as the row index
)
From Excel
df = pd.read_excel('sales_report.xlsx', sheet_name='Data')
Exploring a DataFrame
Always start by exploring the shape and contents of a new dataset:
df.shape # (rows, columns) -- e.g., (50000, 12)
df.head(5) # first 5 rows
df.tail(5) # last 5 rows
df.info() # column names, data types, non-null counts
df.describe() # statistical summary for numeric columns
df.columns # list of column names
df.dtypes # data type of each column
df.isnull().sum() # count of missing values per column
Selecting Data
Selecting Columns
# Single column (returns a Series)
df['revenue']
df.revenue # dot notation (avoid if column name has spaces)
# Multiple columns (returns a DataFrame)
df[['name', 'revenue', 'orders']]
Selecting Rows with .loc and .iloc
# .loc: label-based selection
df.loc[0] # row with index label 0
df.loc[0:5] # rows with index labels 0 to 5 (inclusive)
df.loc[0, 'revenue'] # specific cell by row label and column name
df.loc[:, 'revenue'] # all rows, revenue column
# .iloc: position-based selection
df.iloc[0] # first row
df.iloc[0:5] # rows 0 to 4 (exclusive end)
df.iloc[0, 3] # row 0, column 3 (4th column)
Filtering Rows (Boolean Indexing)
# Single condition
df[df['revenue'] > 500]
# Multiple conditions (use & for AND, | for OR)
df[(df['country'] == 'Nigeria') & (df['revenue'] > 500)]
df[(df['country'] == 'Nigeria') | (df['country'] == 'Ghana')]
# Using isin() for multiple values
df[df['country'].isin(['Nigeria', 'Ghana', 'Kenya'])]
# Checking for NaN
df[df['email'].isna()]
df[df['email'].notna()]
Modifying DataFrames
Adding and Modifying Columns
# Add a new column
df['revenue_per_order'] = df['revenue'] / df['orders']
df['is_high_value'] = df['revenue'] > 1000
# Apply a function to create a new column
def categorise(revenue):
if revenue >= 1000:
return 'VIP'
elif revenue >= 500:
return 'Regular'
else:
return 'Low'
df['segment'] = df['revenue'].apply(categorise)
# Using numpy where (faster for simple conditions)
df['tier'] = np.where(df['revenue'] >= 500, 'High', 'Low')
Renaming Columns
df.rename(columns={'revenue': 'total_revenue', 'orders': 'order_count'}, inplace=True)
Dropping Columns
df.drop(columns=['column_to_drop'], inplace=True)
df.drop(columns=['col1', 'col2'], inplace=True)
Sorting
df.sort_values('revenue', ascending=False)
# Sort by multiple columns
df.sort_values(['country', 'revenue'], ascending=[True, False])
Aggregation with groupby
pandas groupby is the equivalent of SQL's GROUP BY:
# Group by one column
df.groupby('country')['revenue'].sum()
# Group by one column, multiple aggregations
df.groupby('country').agg(
total_revenue=('revenue', 'sum'),
avg_revenue=('revenue', 'mean'),
order_count=('orders', 'sum'),
customer_count=('name', 'count')
).reset_index()
# Group by multiple columns
df.groupby(['country', 'segment'])['revenue'].sum().reset_index()
Merging DataFrames
pandas merge is equivalent to SQL JOIN:
# INNER JOIN
merged = pd.merge(orders, customers, on='customer_id', how='inner')
# LEFT JOIN
merged = pd.merge(customers, orders, on='customer_id', how='left')
# Merge on columns with different names
merged = pd.merge(
orders,
customers,
left_on='cust_id',
right_on='customer_id',
how='inner'
)
Exporting Results
# Save to CSV
df.to_csv('output.csv', index=False)
# Save to Excel
df.to_excel('output.xlsx', sheet_name='Analysis', index=False)
# Save multiple sheets to one Excel file
with pd.ExcelWriter('report.xlsx') as writer:
summary.to_excel(writer, sheet_name='Summary', index=False)
details.to_excel(writer, sheet_name='Details', index=False)
Key Takeaways
- The pandas DataFrame is the central data structure for Python analysis: tabular data with named columns and row indices.
- Explore any new DataFrame first with .shape, .head(), .info(), .describe(), and .isnull().sum().
- Filter rows using boolean indexing: df[df['column'] > value]. Combine conditions with & (AND) and | (OR).
- groupby() is pandas' equivalent of SQL GROUP BY -- use it with .agg() to calculate multiple aggregations at once.
- pd.merge() performs SQL-style joins between DataFrames using how='inner', 'left', 'right', or 'outer'.
Practice Exercise
import pandas as pd
# Load sample data
url = 'https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'
df = pd.read_csv(url)
# 1. Explore: shape, info, describe, isnull().sum()
print(df.shape)
print(df.isnull().sum())
# 2. Select only Name, Age, Fare, Survived columns
subset = df[['Name', 'Age', 'Fare', 'Survived']]
# 3. Filter to passengers who survived and paid more than $50
survivors_high_fare = df[(df['Survived'] == 1) & (df['Fare'] > 50)]
# 4. Add a column categorising fare as 'Low', 'Medium', 'High'
df['fare_tier'] = pd.cut(df['Fare'], bins=[0, 20, 100, 1000], labels=['Low', 'Medium', 'High'])
# 5. Group by survival status and calculate mean age and fare
df.groupby('Survived').agg(avg_age=('Age', 'mean'), avg_fare=('Fare', 'mean'))
Try it yourself
Key Takeaways
- The pandas DataFrame is the core data structure for Python analysis -- load data with pd.read_csv() or pd.read_excel().
- Always explore a new DataFrame with .shape, .head(), .info(), .describe(), and .isnull().sum() before any analysis.
- Filter rows using boolean indexing with & (AND) and | (OR) -- each condition must be wrapped in parentheses.
- groupby().agg() is the pandas equivalent of SQL GROUP BY with named aggregation outputs and .reset_index() to flatten the result.
- pd.merge() performs SQL-style joins between DataFrames with how='inner', 'left', 'right', or 'outer'.
Quick Quiz
1.What does df.info() show about a DataFrame?
2.How do you filter a pandas DataFrame to rows where column 'country' is 'Nigeria' AND column 'revenue' is greater than 500?
3.What is the pandas equivalent of SQL's GROUP BY with multiple aggregations?
4.What is the difference between .loc and .iloc in pandas?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx