<jupyter_start><jupyter_text>Pyber Challenge<jupyter_code># Add Matplotlib inline magic command
%matplotlib inline
import matplotlib as plt
import pandas as pd
city_data_to_load = "./Resources/city_data.csv"
ride_data_to_load = "./Resources/ride_data.csv"
# Read the city and ride data
city_data_df = pd.read_csv(city_data_to_load)
ride_data_df = pd.read_csv(ride_data_to_load)
city_data_df
ride_data_df
# Mereg the DataFrame
pyber_data_df = pd.merge(ride_data_df,city_data_df, how="left", on="city")
pyber_data_df<jupyter_output><empty_output><jupyter_text>Challenge 1 Get a Summary DataFrame<jupyter_code># 1. Get total rides for each city type
total_rides_by_type = pyber_data_df.groupby(["type"]).count()["ride_id"]
total_rides_by_type
# 2. Get total drivers for each city type
total_drivers_by_type = city_data_df.groupby(["type"]).sum()["driver_count"]
total_drivers_by_type
#3. Get the total amount of fare for each city type
total_fare_by_type = pyber_data_df.groupby(["type"]).sum()["fare"]
total_fare_by_type
#4. Get the average fare per ride for each city type
avg_fare_per_ride = total_fare_by_type / total_rides_by_type
avg_fare_per_ride
#5. Get the average fare per driver for each city type
avg_fare_per_driver = total_fare_by_type / total_drivers_by_type
avg_fare_per_driver
summary = {
    "Total Rides": total_rides_by_type,
    "Total Drivers": total_drivers_by_type,
    "Total Fares": total_fare_by_type,
    "Average Fare per Ride": avg_fare_per_ride,
    "Average Fare per Driver": avg_fare_per_driver
}
pyber_ride_summary_df = pd.DataFrame(summary)
pyber_ride_summary_df
#7 delete the index name
#del pyber_ride_summary_df.index.name
pyber_ride_summary_df.index.name =""
pyber_ride_summary_df
#8. Format the columns
pyber_ride_summary_df["Total Rides"] = pyber_ride_summary_df["Total Rides"].map("{:,}".format)
pyber_ride_summary_df["Total Drivers"] = pyber_ride_summary_df["Total Drivers"].map("{:,}".format)
pyber_ride_summary_df["Total Fares"] = pyber_ride_summary_df["Total Fares"].map("${:,.2f}".format)
pyber_ride_summary_df["Average Fare per Ride"] = pyber_ride_summary_df["Average Fare per Ride"].map("${:,.2f}".format)
pyber_ride_summary_df["Average Fare per Driver"] = pyber_ride_summary_df["Average Fare per Driver"].map("${:,.2f}".format)
pyber_ride_summary_df<jupyter_output><empty_output><jupyter_text>Challenge 2 Create a line plot that shows the sum of the fares for each type of city<jupyter_code>#1. Rename columns
pyber_data_df = pyber_data_df.rename(columns={
    "city": "city",
    "date": "Date",
    "fare": "Fare",
    "ride_id": "Ride id",
    "driver_count": "No. Drivers",
    "type": "City Type"   
})
pyber_data_df
# 2 Set the index to the "Date" column
pyber_data_df.set_index(pyber_data_df["Date"], inplace=True)
pyber_data_df
#3. Make new df for fares and include only the Date CityType and Fare column
column_names = ["Date", "City Type", "Fare"]
pyber_cities_fare = pyber_data_df[column_names].copy()
pyber_cities_fare
# 4. Drop the extra columns
pyber_cities_fare.drop(["Date"], axis=1, inplace=True)
pyber_cities_fare.index = pd.to_datetime(pyber_data_df.index)
pyber_cities_fare.head()
pyber_cities_fare.info()
#7. Calculate the sum of fares by the type of the city and date using groupbyto create a Series
sum_fare_by_type = pyber_cities_fare.groupby(["City Type", "Date"]).sum()["Fare"]
sum_fare_by_type

# 7b Alternatively you can create a DataFramy by using double brakets
sum_fare_by_type = pyber_cities_fare.groupby(["City Type", "Date"]).sum()[["Fare"]]
sum_fare_by_type

# Convert using the pd.DataFrame method
sum_fare_by_type = pd.DataFrame(sum_fare_by_type)
sum_fare_by_type
#9. Reset index so we can pivot method in pandas
sum_fare_by_type = sum_fare_by_type.reset_index()
sum_fare_by_type

#10. Create a pivot table with the date as the index and columns as the type with fare in each row
sum_fare_by_type_pivot = sum_fare_by_type.pivot(index="Date", columns="City Type")["Fare"]
sum_fare_by_type_pivot.head(10)

sum_fare_by_type_pivot.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 2375 entries, 2019-01-01 00:08:16 to 2019-05-08 13:10:18
Data columns (total 3 columns):
 #   Column    Non-Null Count  Dtype  
---  ------    --------------  -----  
 0   Rural     125 non-null    float64
 1   Suburban  625 non-null    float64
 2   Urban     1625 non-null   float64
dtypes: float64(3)
memory usage: 74.2 KB<jupyter_text>Create a line chart that shows fares from Jan 1, 2018 to April 29, 2018<jupyter_code>fare_Jan_April = sum_fare_by_type_pivot.loc['2019-01-01': '2019-04-28']
fare_Jan_April.head(20)
#12 Create a new df and use resample by week and get sum of the fares for each week
weekly_fares_df = fare_Jan_April.resample("W").sum()
weekly_fares_df
# 13 Use axes plotting to add labels and titles
from matplotlib import style
import matplotlib.pyplot as plt
style.use("fivethirtyeight")
ax = weekly_fares_df.plot(figsize=(20,6))
#Add a title
ax.set_title("Total Fare by City Type")
# Add a x- and y-axis label
ax.set_xlabel("Month")
ax.set_ylabel("Fare $USD")
plt.savefig("Challenge_fare_summary.png")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11162880993981751, 'fraction_numerical': 0.022908173170258204, 'mean_word_length': 6.0095238095238095, 'pattern_counts': {'":': 11, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '540226', 'license': 'MIT', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1898, 'n_words': 465, 'path': 'Pyber_Challenge.ipynb', 'repo_name': 'Kbusiness9/Pyber_Challenge', 'size': '5151'}