Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import pandas as pd
# Step 1: Load Data
data = pd.read_csv("trips_by_distance.csv")
# Step 2: Data Pre-processing
# Handle Missing Values
# Remove Duplicates
# Validate Data (if necessary)
# Step 3: Data Categorization
# Identify movements away from home
# For example, filter rows where "Population Not Staying at Home" is nonzero
away_from_home_data = data[data['Population Not Staying at Home'] > 0]
# Step 4: Data Aggregation
# Calculate the total distance traveled by individuals when away from home
# We'll sum up the products of the number of trips in each distance range by their respective midpoint
total_distance_away_from_home = (
(away_from_home_data['Number of Trips <1'] * 0.5) +
(away_from_home_data['Number of Trips 1-3'] * 2) +
(away_from_home_data['Number of Trips 3-5'] * 4) +
(away_from_home_data['Number of Trips 5-10'] * 7.5) +
(away_from_home_data['Number of Trips 10-25'] * 17.5) +
(away_from_home_data['Number of Trips 25-50'] * 37.5) +
(away_from_home_data['Number of Trips 50-100'] * 75) +
(away_from_home_data['Number of Trips 100-250'] * 175) +
(away_from_home_data['Number of Trips 250-500'] * 375) +
(away_from_home_data['Number of Trips >=500'] * 750)
)
# Calculate the total number of trips when away from home
total_trips_away_from_home = away_from_home_data['Number of Trips'].sum()
# Calculate the average distance traveled per trip when away from home
average_distance_per_trip_away_from_home = total_distance_away_from_home.sum() / total_trips_away_from_home
# Display the Result
print("Average distance traveled per trip when away from home:", average_distance_per_trip_away_from_home)