<jupyter_start><jupyter_text>Project: Investigate a Dataset (FBI gun data) Table of ContentsIntroductionData WranglingExploratory Data AnalysisConclusions<jupyter_code>#<img src='OneDrive/Desktop/gun.png'>
from IPython.display import Image
Image(filename="OneDrive/Desktop/gun.png", width=1050, height=0)<jupyter_output><empty_output><jupyter_text>Introduction**"The National Instant Criminal Background Check System, or NICS, is all about saving lives and protecting people from harm—by not letting guns fall into the wrong hands. It also ensures the timely transfer of firearms to eligible gun buyers. Mandated by the Brady Handgun Violence Prevention Act of 1993 and launched by the FBI on November 30, 1998, NICS is used by Federal Firearms Licensees (FFLs) to instantly determine whether a prospective buyer is eligible to buy firearms. Before ringing up the sale, cashiers call in a check to the FBI or to other designated agencies to ensure that each customer does not have a criminal record or isn’t otherwise ineligible to make a purchase. More than 230 million such checks have been made, leading to more than 1.3 million denials.NICS is located at the FBI’s Criminal Justice Information Services Division in Clarksburg, West Virginia. It provides full service to FFLs in 30 states, five U.S. territories, and the District of Columbia. Upon completion of the required Bureau of Alcohol, Tobacco, Firearms and Explosives (ATF) Form 4473, FFLs contact the NICS Section via a toll-free telephone number or electronically on the Internet through the NICS E-Check System to request a background check with the descriptive information provided on the ATF Form 4473. NICS is customarily available 17 hours a day, seven days a week, including holidays (except for Christmas)."**<jupyter_code>import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
import seaborn as sns
plt.rcParams['figure.figsize']=(12,7)
import numpy as np
import statsmodels.api as sms;


# Read in the data

df=pd.read_csv('https://raw.githubusercontent.com/BuzzFeedNews/nics-firearm-background-checks/master/data/nics-firearm-background-checks.csv')
census_data=pd.read_csv('OneDrive/Desktop/project/u.s.-census-data.csv')


df.head()
df.info()

# split the data after '-' in month column to year
df['year'] = df['month'].apply(lambda x: x.split("-")[0]).astype(int)

#split the data after '-' in year column to month
df['months'] = df['month'].apply(lambda x: x.split("-")[1]).astype(int)

df.head(1)

census_data.head()
# Drop unsed column such as fact Note column.

df=df.drop('month',axis=1)
df.head()<jupyter_output><empty_output><jupyter_text>Data Wrangling**In this section of the report, we will check for cleanliness,we will  trim and clean our dataset for analysis.** drop rows with missing values<jupyter_code>df.isnull().sum()
census_data.isnull().sum()
#Drop null values from each file
df.dropna(inplace=True)
census_data.dropna(inplace=True)
#check for null values, the result sould be False
df.isnull().sum().any()
#check for null values, the result sould be False
census_data.isnull().sum().any()<jupyter_output><empty_output><jupyter_text>Data Cleaning (for Nics-Firearm-Background, Gun_Data, and Census_Data) In this part we will clean our data by remove null and duplicate values in each file<jupyter_code>#print duplicated values, if the result =0, there is no need to  use method drop_duplicatesprint(df.duplicated().sum())
print(df.duplicated().sum())
print(census_data.duplicated().sum())

# Drop unsed column such as fact Note column.

census_data =census_data.drop('Fact Note',axis=1)
#Check for null values after drop fact note
census_data.isnull().sum().sum()
census_data.head()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis Question 1: which state get the highest number of Hispanic or Latino, percent, July 1, 2016,  (V2016)  of census data ?<jupyter_code>#Index fact
census_data.set_index('Fact',inplace=True)
census_data = census_data.T.reset_index()
census_data
#rename the coulumn to (state)
census_data.rename(columns={'index':'state'},inplace = True)
census_data.head()
#convert srt to float datatype
census =  census_data.iloc[:,1:].columns
for c in census:
     census_data[c] =  census_data[c].str.extract('(\d+)').astype(float)
    
# check the changes
census_data.info()

for column in census_data.columns:
    if 'percent' in column:
        census_data[column] = census_data[column]/100
census_data.describe()
#change the name of new_labels = list(census_data.columns)
new_labels = list(census_data.columns)
new_labels[2] ='Black or African American alone percent V2016'
census_data.columns = new_labels
new_labels = list(census_data.columns)
new_labels[3] ='American Indian and Alaska Native alone percent July V2016'
census_data.columns = new_labels
new_labels = list(census_data.columns)
new_labels[4] ='Asian alone, percent July V2016'
census_data.columns = new_labels
new_labels = list(census_data.columns)
new_labels[5] ='Native Hawaiian and Other Pacific Islander alone, percent July 2016 V2016'
census_data.columns = new_labels
new_labels = list(census_data.columns)
new_labels[6] ='Hispanic or Latino, percent July 2016 V2016'
census_data.columns = new_labels
new_labels = list(census_data.columns)
new_labels[7] ='Total accommodation and food services sales 2012'
census_data.columns = new_labels
census_data.head()
#total_gun = df.groupby(df['year'])['totals'].sum().reset_index()
#plt.subplots(figsize = (10,5))
#plt.bar(total_gun['year'],total_gun['totals'])
#plt.title('Number of Gun Registrations Per Year 2015-2018')
#plt.xlabel('Year')
#plt.ylabel('Total # of Gun Permits Issued')
#merge Two datasets
df_combined = df.merge(census_data, left_on='state', right_on='state', how='inner')
df_combined.head()
df_combined.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 1400 entries, 0 to 1399
Data columns (total 44 columns):
state                                                                        1400 non-null object
permit                                                                       1400 non-null float64
permit_recheck                                                               1400 non-null float64
handgun                                                                      1400 non-null float64
long_gun                                                                     1400 non-null float64
other                                                                        1400 non-null float64
multiple                                                                     1400 non-null int64
admin                                                                        1400 non-null float64
prepawn_handgun                                                              1400 non-null float64
prepa[...]<jupyter_text>Question 1:**what is the states with the highest number of Hispanic or Latino, percent July 2016 V2016?**<jupyter_code>sns.barplot(y='state', x='Hispanic or Latino, percent July 2016 V2016' ,data=df_combined[:800]).set_title('The states with the highest number of Hispanic or Latino, percent July 2016 V2016');<jupyter_output>C:\Users\farah\Anaconda3\lib\site-packages\scipy\stats\stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>Answer:**After the data become ready for display, we wanted to know which state contains the highest number of Hispanic or Latino, percent July 2016 V2016.As the graph illustrates, California has the largest number of them.**   Question 2: In this question, we would like to know how many handguns are available against the number of states.<jupyter_code>df_total= df_combined.groupby(['state'])['handgun'].mean()
df_total.sort_values()
states = df_total.sort_values(ascending=False).head(100)
states 


#state_And_handgun= df.groupby('state')['handgun'].sum()
fig = plt.figure(figsize=(12,6))
plt.hist(states)
fig.suptitle('Total of handgun and states')
plt.xlabel('count of handguns')
plt.ylabel('count of state')
plt.show();<jupyter_output><empty_output><jupyter_text>Answer :**As we can see, the largest number of handguns was about 5,000. They were recorded in more than seventeen states.**   Question 3: In this question, we would like to know the month which occurred the largest number of long gun purchases?<jupyter_code>total_of_long_gun_per_months = df_combined.groupby('months').sum().long_gun

total_of_long_gun_per_months
total_of_handg_gun_per_months=df_combined.groupby('months').sum().handgun
total_of_handg_gun_per_months
plt.figure(figsize=(20,8))
plt.plot(total_of_long_gun_per_months, color='blue',linewidth=5)
plt.xlabel('months')
plt.ylabel('No of long_gun')
plt.title('Total of long gun on each months')
plt.legend()
plt.show;<jupyter_output><empty_output><jupyter_text>Answer: We can notice that month of December recorded the highest purchase of long gun approximately with 2000000 total of purchase.<jupyter_code>gun_alltime = df_combined.groupby(['year', 'state'])['totals'].sum()
gun_alltime<jupyter_output><empty_output><jupyter_text>Question 4:**which state is encompass least affected by total of purchase of gun?**<jupyter_code>total=df_combined.groupby(['state'])['totals'].sum()
bottom10state=total.sort_values(ascending=False).head(10)
bottom10state
plt.figure(figsize=(12,6))
chart=bottom10state.plot(kind="bar", color='#AA0000', title= 'state least affected by total');
chart.set_xlabel('State', fontsize=14);
chart.set_ylabel('Total', fontsize=14);<jupyter_output><empty_output><jupyter_text>Answer :**As we can see,the state encompass least affected by total is  Minnesota with 1534899.**<jupyter_code>#total_by_state=df_combined.groupby(['state'])['White alone', 'Black or African American alone', 'American Indian and Alaska Native alone','Asian alone','Native Hawaiian and Other Pacific Islander alone','Hispanic or Latino','totals'].sum()
#least=total_by_state.sort_values('totals').head(10)
#least<jupyter_output><empty_output><jupyter_text>Question 5: What is highest gun purchase for each prospective buyer ?<jupyter_code>new_labels = list(df_combined.columns)
new_labels[28] ='White alone'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)
new_labels[29] ='Black or African American alone'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)
new_labels[30] ='American Indian and Alaska Native alone'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)
new_labels = list(df_combined.columns)
new_labels[31] ='Asian alone'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)

new_labels = list(df_combined.columns)
new_labels[32] ='Native Hawaiian and Other Pacific Islander alone'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)
new_labels = list(df_combined.columns)
new_labels[33] ='Hispanic or Latino'
df_combined.columns = new_labels
new_labels = list(df_combined.columns)

sns.set_style('darkgrid')
f, ax = plt.subplots(1,figsize=(20,15))
bar_1=[i+1 for i in range(len(least['White alone']))]

ax.bar(bar_1, least['White alone'], label ='White alone', color='powderblue', width=0.8)
ax.bar(bar_1, least['Black or African American alone'],bottom=least['White alone'], label ='Black or African American alone', color='cadetblue', width=0.8)
ax.bar(bar_1, least['American Indian and Alaska Native alone'], bottom=[i+j for i,j in zip(least['White alone'], least['Black or African American alone'])], label ='American Indian and Alaska Native alone', color='silver', width=0.8) 

ax.bar(bar_1, least['Asian alone'], bottom=[i+j+k for i,j,k in zip(least['White alone'], least['Black or African American alone'], least['American Indian and Alaska Native alone'])], label ='Asian alone', color='dodgerblue', width=0.8) 

ax.bar(bar_1, least['Native Hawaiian and Other Pacific Islander alone'], bottom=[i+j+k+l for i,j,k,l in zip(least['White alone'], least['Black or African American alone'],least['American Indian and Alaska Native alone'],least['Asian alone'])], label ='Native Hawaiian and Other Pacific Islander alone', color='yellowgreen', width=0.8) 

ax.bar(bar_1, least['Hispanic or Latino'], bottom=[i+j+k+l+m for i,j,k,l,m in zip(least['White alone'], least['Black or African American alone'],least['American Indian and Alaska Native alone'],least['Asian alone'],least['Native Hawaiian and Other Pacific Islander alone'])], label ='Hispanic or Latino', color='darkblue', width=0.8) 

ax.set_title("the affected by state")
ax.set_xlabel("states")
ax.set_ylabel("#NO of affected")

plt.legend(loc='upper left');<jupyter_output><empty_output><jupyter_text>Answer :**From the previous chart we can notice that(Hispanic or Latino) with least number on each state but recorded the high purchase of gun. On the other hand (white alone) recorded the highest number of gun purchase on each state**.  Conclusions**This analysis helped us to discover the whole idea of FBI guns in the US state. The data allowed us to observe trends of Purchases of guns between each state and what we found from these data: from asking the questions and working with data I found the answers that I worked for, which is about the states and months recorded the highest purchases of guns.**  Resources1- https://www.fbi.gov/services/cjis/nics<jupyter_code>df.to_csv('df.csv', index=False)
census_data.to_csv('census_data.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10653222270933801, 'fraction_numerical': 0.021993277802133566, 'mean_word_length': 4.943117672600955, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '445042', 'license': 'FTL', 'n_tokens_mistral': 4357, 'n_tokens_neox': 4023, 'n_words': 1306, 'path': 'FBI Gun data project  DAND.ipynb', 'repo_name': 'faro7ah/FBI-gun-project-DAND', 'size': '13686'}