<jupyter_start><jupyter_text>Use Pandas read_csv to load the us-counties.csv file.The columns of the CSV file are:date   - The date as a string (don't forget to convert this to a `pd.datetime`)county - The name of the countystate  - The name of the statefips   - The fips code of the county (the first two digits represent the state, the last three, the county)cases  - The cumulative number of cases at the date in the countydeaths - The cumulative number of deaths in at the date in the county<jupyter_code>us_df = pd.read_csv("us-counties.csv")
us_df['date'] = pd.to_datetime(us_df['date'])
# just to have an over all idea on how the actual table looks
# tests<jupyter_output><empty_output><jupyter_text>Question 1: Produce a DataFrame that contains only the data for the top 10 Counties by total number of cases<jupyter_code>top_counties = us_df.groupby(['county', 'state'], as_index=False).max()

# important to restreict the values to 10 here otherwise the code will err in question3 due to performance!!
top_counties = top_counties.sort_values(by='cases', ascending = False).head(10)
top_counties<jupyter_output><empty_output><jupyter_text>Question 2: Produce a DataFrame that contains only the data for the top 5 states by total number of cases<jupyter_code>#removing county column and selecting others to just keep the table focused on state level metric
#approach similar to question 1
top_state = us_df.groupby('state')[['cases','deaths','date']].max()

top_state = top_state.sort_values(by='cases', ascending=False)
top_state.head(n=5)<jupyter_output><empty_output><jupyter_text>Question 3: Calculate the incidence (the number of new cases per day... i.e. the difference between the cases on a day - the cases on a previous day) for the top 10 Counties.<jupyter_code>top_county_spike=us_df[(us_df['county'].isin(top_counties['county'])) & (us_df['state'].isin(top_counties['state']))]

def calc_incidences(row, top_county_spike):
    prev_index = (row.name[0], pd.to_datetime(row.name[1])-pd.offsets.Day(1))
    if prev_index in top_county_spike.index: 
        return row['cases'] - top_county_spike.loc[prev_index]['cases']
    else:
        return 0

top_county_spike = top_county_spike.set_index(['county','date'])
top_county_spike['incidences'] = top_county_spike.apply(lambda x: calc_incidences(x,top_county_spike),axis=1)
top_county_spike.sort_values('incidences', ascending=False).head(n=10)<jupyter_output><empty_output><jupyter_text>Question 4: Calculate the peak incidence for the 10 counties (the day with the highest number of new cases).<jupyter_code>top_county_spike = top_county_spike.reset_index()
p_df = top_county_spike.iloc[top_county_spike.groupby('county')['incidences'].idxmax()]
p_df = p_df.set_index('county')
p_df = p_df.sort_values(by = 'incidences', ascending=False)
p_df.head(n=10)<jupyter_output><empty_output><jupyter_text>Question 5: Plot an incidence curve for the County with the highest peak.<jupyter_code>top_county_spike= top_county_spike.set_index(['county','date'])
plotit = top_county_spike.loc['Los Angeles']['incidences'].plot()
plotit.set_ylabel("Incidences")
plotit<jupyter_output><empty_output><jupyter_text>Question 6: Plot a vertical bar graph with County as the category and the peak incidence as the measure.<jupyter_code>p_df = p_df.head(n=10)
incidence = p_df[['incidences']].plot.bar()
incidence.plot()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11101295641931684, 'fraction_numerical': 0.009128386336866903, 'mean_word_length': 6.720454545454546, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '17482', 'license': 'CC0-1.0', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1134, 'n_words': 309, 'path': 'US County Data - pandas.ipynb', 'repo_name': 'nikhil1007/ClassAssignmentNo4', 'size': '3396'}