-
Notifications
You must be signed in to change notification settings - Fork 1
/
process_data.py
74 lines (55 loc) · 2.56 KB
/
process_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# import libraries
import sys
import pandas as pd
from sqlalchemy import create_engine
def load_data(messages_filepath, categories_filepath):
messages = pd.read_csv(messages_filepath)
categories = pd.read_csv(categories_filepath)
df = pd.merge(messages, categories, on="id")
return df
def clean_data(df):
# create a dataframe of the 36 individual category columns
categories = df['categories'].str.split(";", expand=True)
# select the first row of the categories dataframe
row = categories.loc[0, ]
# use this row to extract a list of new column names for categories.
# one way is to apply a lambda function that takes everything
# up to the second to last character of each string with slicing
category_colnames = row.apply(lambda x: x.split("-")[0])
categories.columns = category_colnames
for column in categories:
# set each value to be the last character of the string
categories[column] = categories[column].apply(
lambda x: x.split("-")[-1])
# convert column from string to numeric
categories[column] = categories[column].astype('int')
# drop the original categories column from `df`
df.drop('categories', inplace=True, axis=1)
# concatenate the original dataframe with the new `categories` dataframe
df = pd.concat([df, categories], axis=1)
# drop duplicates
df = df.drop_duplicates(subset=['message'])
return df
def save_data(df, database_filename):
engine = create_engine('sqlite:///' + database_filename)
df.to_sql('message_table', engine, index=False)
def main():
if len(sys.argv) == 4:
messages_filepath, categories_filepath, database_filepath = sys.argv[1:]
print('Loading data...\n MESSAGES: {}\n CATEGORIES: {}'
.format(messages_filepath, categories_filepath))
df = load_data(messages_filepath, categories_filepath)
print('Cleaning data...')
df = clean_data(df)
print('Saving data...\n DATABASE: {}'.format(database_filepath))
save_data(df, database_filepath)
print('Cleaned data saved to database!')
else:
print('Please provide the filepaths of the messages and categories '
'datasets as the first and second argument respectively, as '
'well as the filepath of the database to save the cleaned data '
'to as the third argument. \n\nExample: python process_data.py '
'disaster_messages.csv disaster_categories.csv '
'DisasterResponse.db')
if __name__ == '__main__':
main()