Skip to the content.

Lists and Filtering

HW

def find_students_in_range(student_data, min_score, max_score):
    return [student for student in student_data if min_score <= student['score'] <= max_score]

# Example student list (list of dictionaries)
student_data = [
    {'name': 'Alice', 'score': 78},
    {'name': 'Bob', 'score': 85},
    {'name': 'Charlie', 'score': 92},
    {'name': 'David', 'score': 88},
    {'name': 'Eve', 'score': 76}
]

# Test
print(find_students_in_range(student_data, 80, 90))
[{'name': 'Bob', 'score': 85}, {'name': 'David', 'score': 88}]
def add_letter_grades(student_data):
    for student in student_data:
        score = student['score']
        if score >= 90:
            student['Letter'] = 'A'
        elif score >= 80:
            student['Letter'] = 'B'
        elif score >= 70:
            student['Letter'] = 'C'
        elif score >= 60:
            student['Letter'] = 'D'
        else:
            student['Letter'] = 'F'
    return student_data

student_data = [
    {'name': 'Alice', 'score': 78},
    {'name': 'Bob', 'score': 85},
    {'name': 'Charlie', 'score': 92},
    {'name': 'David', 'score': 60},
    {'name': 'Eve', 'score': 55}
]


print(add_letter_grades(student_data))
[{'name': 'Alice', 'score': 78, 'Letter': 'C'}, {'name': 'Bob', 'score': 85, 'Letter': 'B'}, {'name': 'Charlie', 'score': 92, 'Letter': 'A'}, {'name': 'David', 'score': 60, 'Letter': 'D'}, {'name': 'Eve', 'score': 55, 'Letter': 'F'}]
from collections import Counter

def find_mode(series):
    counts = Counter(series)
    return counts.most_common(1)[0][0] 

print(find_mode([1, 2, 2, 3, 4, 2, 5]))
2
import pandas as pd
import sqlite3
import matplotlib.pyplot as plt
import seaborn as sns
from collections import Counter

datas = pd.read_csv('data_title.csv')

avg_temp = datas.groupby('incident_id')['temperature'].mean()
highest_avg_temp_incident = avg_temp.idxmax()
lowest_avg_temp_incident = avg_temp.idxmin()
print("Highest avg temp incident ID:", highest_avg_temp_incident)
print("Lowest avg temp incident ID:", lowest_avg_temp_incident)

temp_diff = datas.groupby('incident_id')['temperature'].agg(lambda x: x.max() - x.min())
print("Temperature difference per incident (sample):")
print(temp_diff.head())

overall_avg_temp = datas['temperature'].mean()
above_avg_temp = datas[datas['temperature'] > overall_avg_temp]
print(f"Incidents above overall average temperature ({overall_avg_temp:.2f}°F): {len(above_avg_temp)}")

grouped = datas.groupby(['vegetation_type', 'weather_condition'])[['temperature', 'wind_speed']].mean().reset_index()
print("Grouped by vegetation and weather (sample):")
print(grouped.head())

datas['vegetation_code'] = datas['vegetation_type'].astype('category').cat.codes
correlation = datas[['vegetation_code', 'fire_intensity']].corr()
print("Correlation between vegetation type and fire intensity:")
print(correlation)

sns.scatterplot(data=datas, x='vegetation_code', y='fire_intensity')
plt.title('Vegetation Type vs Fire Intensity')
plt.xlabel('Vegetation Type (coded)')
plt.ylabel('Fire Intensity')
plt.show()

weather_avg_intensity = datas.groupby('weather_condition')['fire_intensity'].mean()
highest_intensity_condition = weather_avg_intensity.idxmax()
print("Weather condition with highest fire intensity:", highest_intensity_condition)

over_100F = datas[datas['temperature'] > 100]
percent_over_100F = (len(over_100F) / len(datas)) * 100
print(f"Percentage of incidents over 100°F: {percent_over_100F:.2f}%")

conn = sqlite3.connect('fire_incidents.db')
datas.to_sql('fire_incidents', conn, if_exists='replace', index=False)

query1 = pd.read_sql_query("""
SELECT vegetation_type, AVG(temperature) AS avg_temp, AVG(wind_speed) AS avg_wind
FROM fire_incidents
GROUP BY vegetation_type
""", conn)
print("SQL - Avg temp and wind per vegetation type:")
print(query1)

query2 = pd.read_sql_query("""
SELECT * FROM fire_incidents
WHERE temperature > 120 AND wind_speed > 15
""", conn)
print("SQL - Incidents with temperature >120°F and wind >15 mph:")
print(query2.head())

query3 = pd.read_sql_query("""
SELECT weather_condition, AVG(fire_intensity) AS avg_intensity
FROM fire_incidents
GROUP BY weather_condition
""", conn)
print("SQL - Avg fire intensity per weather condition:")
print(query3)

print("\n📌 Pandas vs SQL Comparison:")
print("- Pandas is faster for in-memory operations, better for exploratory data analysis.")
print("- SQL is better for handling large data, integrates well with databases.")
print("- Pandas is more flexible for complex calculations and visualizations.")