Skip to content

Instantly share code, notes, and snippets.

@smzn
Created January 4, 2024 03:16
Show Gist options
  • Save smzn/65facb5b539df9ba165ca0cd1c4cc1b8 to your computer and use it in GitHub Desktop.
Save smzn/65facb5b539df9ba165ca0cd1c4cc1b8 to your computer and use it in GitHub Desktop.
the total number of rides for each station and member type combination
# Calculating the total number of rides for each station and member type combination
member_counts = df[df['member_casual'] == 'member'].groupby('start_station_name')['ride_id'].count()
casual_counts = df[df['member_casual'] == 'casual'].groupby('start_station_name')['ride_id'].count()
# Combining the counts for each station
combined_member_counts = member_counts.add(casual_counts, fill_value=0)
# Sorting stations by total rides and selecting the top stations
sorted_combined_member_counts = combined_member_counts.sort_values(ascending=False)
cumulative_percentage_members = sorted_combined_member_counts.cumsum() / sorted_combined_member_counts.sum()
top_stations_members = sorted_combined_member_counts[cumulative_percentage_members <= 0.20]
# Separating the counts for member and casual for the top stations
top_member_counts = member_counts.loc[top_stations_members.index].fillna(0)
top_casual_counts = casual_counts.loc[top_stations_members.index].fillna(0)
# Plotting stacked bar chart
plt.figure(figsize=(15, 10))
top_member_counts.plot(kind='bar', color='skyblue', label='Member')
top_casual_counts.plot(kind='bar', color='green', label='Casual', bottom=top_member_counts)
plt.title('Top Stations by Member Type Usage (Member and Casual)')
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