Essential Python Recursion and Pandas Data Analysis
Recursive Functions in Python
def range_sum(num_list, start, end):
if start > end: # Base case
return 0
else: # Recursive case
return num_list[start] + range_sum(num_list, start + 1, end)
numbers = [2, 4, 6, 8, 10]
print(range_sum(numbers, 1, 3)) # Expected output: 18Tower of Hanoi
def hanoi(n, from_peg, to_peg, temp_peg):
if n > 0:
# Step 1: Move n-1 discs to temp peg
hanoi(n - 1, from_peg, temp_peg, to_peg)
# Step 2: Move the bottom disc
print(f"Move disc from peg {from_peg} to peg {to_peg}")
# Step 3: Move n-1 discs from temp peg to target peg
hanoi(n - 1, temp_peg, to_peg, from_peg)Greatest Common Divisor
def gcd(x, y):
if x % y == 0:
return y
else:
return gcd(y, x % y)
print(gcd(48, 18))Fibonacci Sequence
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(5))Factorial Calculation
def factorial(n):
if n == 0: # Base case
return 1
else: # Recursive case
return n * factorial(n - 1)
print(factorial(4))Pandas Data Analysis
import pandas as pd
# From CSV file
df = pd.read_csv("data.csv")
# Look at first rows
print(df.head())
# Check structure
print(df.info())Accessing Data
df['column']: One column (Series)df[['col1', 'col2']]: Multiple columns (DataFrame)df.loc[0:3, 'col1']: Label-based selectiondf.iloc[0:3, 0:2]: Position-based selection
Filtering Rows
df[df['age'] > 30]
df[(df['age'] > 30) & (df['city'] == 'Toronto')]Creating Columns
df['double_age'] = df['age'] * 2
df['bonus'] = df['salary'].apply(lambda x: x * 0.1)Merging and Joining
pd.merge(df1, df2, on='id', how='inner')
pd.merge(df1, df2, left_on='A', right_on='B', how='outer')Grouping Data
df.groupby('department')['salary'].mean()
df.groupby('category').agg({'sales': 'sum', 'profit': 'mean'})Practical Example
Get the rows for people in Toronto who are older than 30, returning only the name and spend columns:
import pandas as pd
df = pd.DataFrame({
"name": ["Ava", "Ben", "Chen", "Devi", "Eli", "Fatima"],
"city": ["Toronto", "Ottawa", "Toronto", "Montreal", "Toronto", "Calgary"],
"age": [28, 34, 41, 22, 36, 29],
"spend": [120, 80, 200, 60, 150, 90]
})
df[(df['city'] == 'Toronto') & (df['age'] > 30)][['name', 'spend']]
English with a size of 2.86 KB