-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaadhaar_data_aggregate.py
More file actions
46 lines (39 loc) · 1.86 KB
/
aadhaar_data_aggregate.py
File metadata and controls
46 lines (39 loc) · 1.86 KB
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
import pandas as pd
import pandasql
def aggregate_query(aadhaar_data):
# Read in our aadhaar_data csv to a pandas dataframe. Afterwards, we rename the columns
# by replacing spaces with underscores and setting all characters to lowercase, so the
# column names more closely resemble columns names one might find in a table.
# Write a query that will select from the aadhaar_data table how many men and how
# many women over the age of 50 have had aadhaar generated for them in each district.
# aadhaar_generated is a column in the Aadhaar Data that denotes the number who have had
# aadhaar generated in each row of the table.
#
# Note that in this quiz, the SQL query keywords are case sensitive.
# For example, if you want to do a sum make sure you type 'sum' rather than 'SUM'.
#
# The possible columns to select from aadhaar data are:
# 1) registrar
# 2) enrolment_agency
# 3) state
# 4) district
# 5) sub_district
# 6) pin_code
# 7) gender
# 8) age
# 9) aadhaar_generated
# 10) enrolment_rejected
# 11) residents_providing_email,
# 12) residents_providing_mobile_number
#
# You can download a copy of the aadhaar data that we are passing
# into this exercise below:
# https://s3.amazonaws.com/content.udacity-data.com/courses/ud359/aadhaar_data.csv
q = """select district,gender, count(gender)as count from aadhaar_data where age>50 group by district,Gender """ # your code here
# Execute your SQL command against the pandas frame
aadhaar_solution = pandasql.sqldf(q.lower(), locals())
return aadhaar_solution
if __name__=='__main__':
aadhaar_data = pd.read_csv('aadhaar_data.csv')
aadhaar_data.rename(columns=lambda x: x.replace(' ', '_').lower(), inplace=True)
print(aggregate_query(aadhaar_data))