Skip to content

Instantly share code, notes, and snippets.

@smzn
Created January 4, 2024 03:10
Show Gist options
  • Save smzn/444f620397c9170f6d9446e4a9e75aef to your computer and use it in GitHub Desktop.
Save smzn/444f620397c9170f6d9446e4a9e75aef to your computer and use it in GitHub Desktop.
the total number of rides for each station and bike type combination
# Calculating the total number of rides for each station and bike type combination
electric_bike_counts = df[df['rideable_type'] == 'electric_bike'].groupby('start_station_name')['ride_id'].count()
classic_bike_counts = df[df['rideable_type'] == 'classic_bike'].groupby('start_station_name')['ride_id'].count()
# Combining the counts for each station
combined_bike_counts = electric_bike_counts.add(classic_bike_counts, fill_value=0)
# Sorting stations by total rides and selecting the top stations
sorted_combined_counts = combined_bike_counts.sort_values(ascending=False)
cumulative_percentage_bikes = sorted_combined_counts.cumsum() / sorted_combined_counts.sum()
top_stations_bikes = sorted_combined_counts[cumulative_percentage_bikes <= 0.20]
# Separating the counts for electric and classic bikes for the top stations
top_electric_counts = electric_bike_counts.loc[top_stations_bikes.index].fillna(0)
top_classic_counts = classic_bike_counts.loc[top_stations_bikes.index].fillna(0)
# Plotting stacked bar chart
plt.figure(figsize=(15, 10))
top_electric_counts.plot(kind='bar', color='skyblue', label='Electric Bike')
top_classic_counts.plot(kind='bar', color='green', label='Classic Bike', bottom=top_electric_counts)
plt.title('Top Stations by Bike Type Usage (Electric and Classic)')
plt.xlabel('Station Name')
plt.ylabel('Total Rides')
plt.xticks(rotation=90)
plt.legend()
plt.grid(axis='y')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment