Permalink
Cannot retrieve contributors at this time
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?
5011-BIG-DATA/numbeer1ii.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 lines (32 sloc)
1.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |