<jupyter_start><jupyter_text>Crime Rates Dataset  Load the dataset via a URL<jupyter_code>import pandas as pd
pd.options.display.float_format = "{:,.2f}".format
crime_rates = pd.read_csv('https://raw.githubusercontent.com/' +
                      'csbfx/advpy122-data/master/crime_rates.csv')<jupyter_output><empty_output><jupyter_text>View some of the rows<jupyter_code>crime_rates.head()
crime_rates.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 2200 entries, 0 to 2199
Data columns (total 12 columns):
 #   Column                    Non-Null Count  Dtype  
---  ------                    --------------  -----  
 0   State                     2200 non-null   object 
 1   Year                      2200 non-null   int64  
 2   Population                2200 non-null   int64  
 3   Violent Crime Rate        2200 non-null   float64
 4   Murder Rate               2200 non-null   float64
 5   Forcible Rape Rate        2200 non-null   float64
 6   Robbery Rate              2200 non-null   float64
 7   Aggraveted Assault Rate   2200 non-null   float64
 8   Property Crime Rate       2200 non-null   float64
 9   Burglary Rate             2200 non-null   float64
 10  Larceny & Theft Rate      2200 non-null   float64
 11  Motor Vehicle Theft Rate  2200 non-null   float64
dtypes: float64(9), int64(2), object(1)
memory usage: 206.4+ KB<jupyter_text>Customize the column names<jupyter_code>crime_rates.columns = ['state', 'year', 'pop', 'violent', 'murder', \
                       'rape', 'robbery', 'assault', 'property', \
                       'burglary', 'larceny','vehicle']
crime_rates.head(3)<jupyter_output><empty_output><jupyter_text>Select data from DataFrame Select rows with specific column value<jupyter_code>alaska_crimes = crime_rates[crime_rates['state']=='Alaska']
alaska_crimes.head(5)<jupyter_output><empty_output><jupyter_text>Indexing in pandasThe indexing operator and attribute selection are nice because they work just like they do in the rest of the Python ecosystem. As a novice, this makes them easy to pick up and use. However, pandas has its own accessor operators, `iloc` and `loc`. For more advanced operations, these are the ones you're supposed to be using. Index-based selectionPandas indexing works in one of two paradigms. The first is index-based selection: selecting data based on its numerical position in the data.`iloc` follows this paradigm.To select the first row of data in a DataFrame, we may use the following:<jupyter_code>crime_rates.iloc[0] # first row

# python index
#[start:stop:skip] # stop is exclusive [1:3] --> index 1 & 2.
#[1::2, :] # from the 2nd row all the way to the end every other row, and all the columns
#[:, :3] # all rows, from the beginning to the 3rd column (0, 1, 2)

crime_rates.iloc[0:10:2, 3:5]<jupyter_output><empty_output><jupyter_text>Both `loc` and `iloc` are row-first, column-second. This is the opposite of what we do in native Python, which is column-first, row-second.This means that it's marginally easier to retrieve rows, and marginally harder to get retrieve columns. To get a column with `iloc`, we can do the following:<jupyter_code>crime_rates.iloc[:, 0] # all rows, first column<jupyter_output><empty_output><jupyter_text>On its own, the `:` operator, which also comes from native Python, means "everything". When combined with other selectors, however, it can be used to indicate a range of values. For example, to select the country column from just the first, second, and third row, we would do:<jupyter_code>crime_rates.iloc[:3, 3:]<jupyter_output><empty_output><jupyter_text>Label-based selectionThe second paradigm for attribute selection is the one followed by the loc operator: label-based selection. In this paradigm, it's the data index value, not its position, which matters.<jupyter_code>crime_rates.loc[:, ['year', 'state', 'pop']]<jupyter_output><empty_output><jupyter_text>Simple data analysisLet's take a look at the crime rates in Alaska throughout the years.<jupyter_code># Select crimes data for state of Alaska
alaska_crimes = crime_rates[crime_rates['state']=='Alaska']
# Set the year as the index of the DataFrame
alaska_crimes_by_year = alaska_crimes.set_index('year')
alaska_crimes_by_year.head(5)
# Use iloc to select all rows and all the columns about crime rates
# Use plot.line() to create a line plot
lines = alaska_crimes_by_year.iloc[:,2:].plot.line()<jupyter_output><empty_output><jupyter_text>Your turn.- Select data from crime_rates dataframe where state equals to "California" and year is between 1980 and 2003, inclusively. *Hint: Use `&` to combine two conditions*. - Create a line plot where the horizontal axis is year and vertical axis is the number of crime cases.<jupyter_code># Your code here . . .
california_crimes_80_03 = crime_rates[(crime_rates.state == 'California') & (crime_rates.year.between(1980, 2003, inclusive= True))]
california_crimes_80_03

#california_crimes_80_03.plot.line(x = 'year')
ca_crimes_by_year = california_crimes_80_03.set_index('year')
line = ca_crimes_by_year.iloc[:,2:].plot.line()<jupyter_output><empty_output><jupyter_text>It's still your turn ... to make another plot* We can't clearly see the trends for the lines that are below 1000  on the y-axis. How about generate another plot, including data for all years, and excluse data for burglary, larceny, and property.<jupyter_code># Your code here . . . 
ca_crimes = crime_rates[crime_rates['state']=='California'].set_index('year')
#ca_crimes_around_1000 = ca_crimes.loc[:, ['violent',	'murder',	'rape',	'robbery','assault','vehicle']].plot.line()
ca_crimes_around_1000 = ca_crimes.drop(columns = ['burglary', 'larceny', 'property']).iloc[:,2:].plot.line()

ca_crimes<jupyter_output><empty_output><jupyter_text>One more plotCreate a line plot for all states to show the California population number over the years. Year will be on horizontal axis and population number will be on the y-axis. Remove the legend from the plot.<jupyter_code># Your code here . . .
#california_pop = ca_crimes.plot(y = 'pop', legend = None)
california_pop = crime_rates[crime_rates['state']=='California'].plot(x = 'year', y = 'pop', legend = None)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10243195361571912, 'fraction_numerical': 0.029312288613303268, 'mean_word_length': 4.316780821917808, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 50, 'https://': 1, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '514885', 'license': 'MIT', 'n_tokens_mistral': 2106, 'n_tokens_neox': 1915, 'n_words': 651, 'path': 'Hands on/CS185C02-Sp21_ho3.ipynb', 'repo_name': 'shahadeshubhu/CS-185C', 'size': '6209'}