text_plain_output_2.png text_plain_output_1.png
import matplotlib.pyplot as plt

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

df = pd.read_csv('../input/adverse-food-events/CAERS_ASCII_2004_2017Q2.csv')

df.shape

df.columns

df.rename(columns={'PRI_Reported Brand/Product Name': 'products_name', 'SYM_One Row Coded Symptoms': 'symptoms', 'CI_Gender': 'gender', 'CI_Age at Adverse Event': 'age', 'CI_Age Unit': 'age_unit', 'RA_Report #': 'no_report', 'RA_CAERS Created Date': 'created_date', 'AEC_Event Start Date': 'start_date', 'PRI_Product Role': 'products_role', 'PRI_FDA Industry Code': 'industry_code', 'AEC_One Row Outcomes': 'Outcomes', 'PRI_FDA Industry Name': 'products_types'}, inplace=True)
df.columns

df['created_date'] = pd.to_datetime(df['created_date'], format='%m/%d/%Y')
df['start_date'] = pd.to_datetime(df['start_date'], format='%m/%d/%Y')

df.duplicated('no_report').value_counts()

df.drop_duplicates('no_report', keep='last', inplace=True)
df.duplicated('no_report').value_counts()

sns.set_style('ticks')
product = df['products_types'].sort_values(ascending=False)
plt.tight_layout()
plt.yticks(fontsize=15)
plt.xticks(fontsize=15)
df.products_types.value_counts().sort_values(ascending=False)

sns.set_style('ticks')
df['products_name'].value_counts()[1:40].sort_values(ascending=True).plot.barh()
plt.tight_layout()
plt.xticks(fontsize=15)
plt.yticks(fontsize=15)
df['products_name'].value_counts()[0:41].sort_values(ascending=False)

symptoms = []
for _, reactions in df['symptoms'].astype(object).str.split(',').iteritems():
    symptoms += [str(l).strip().title() for l in pd.Series(reactions).astype(object)]
outcome_df = pd.DataFrame({'Symptoms': pd.Series(symptoms).value_counts().index, 'Count': pd.Series(symptoms).value_counts()})[:100]
fig, ax = plt.subplots(figsize=(10, 23))
sns.barplot(x='Count', y='Symptoms', data=outcome_df).set_title('Health event counts by product type')