forked from binder-examples/julia-python
/
numpy_intro.py
57 lines (30 loc) · 1.02 KB
/
numpy_intro.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
#!/usr/bin/env python
# coding: utf-8
# This is a simple Python notebook demonstrating the power of numpy
# In[1]:
import pandas as pd
import numpy as np
df = pd.read_csv('2018_General_Election_Returns.csv')
len_df = len(df)
print(f'Imported {len_df} rows')
# In[9]:
def is_state_house(x):
if 'State Rep' in x:
return True
return False
#Finding results for state house
df['is_state_house'] = df['Office'].apply(lambda x: is_state_house(x))
office_df = df[df['is_state_house'] == True]
print(len(office_df))
# In[22]:
print(list(office_df))
rolled = office_df.groupby('Office').sum().reset_index()[['Office', 'Votes']]
rolled = rolled.sort_values(by='Office')
# In[31]:
#Perform some calculations on state house votes
print(len(rolled))
print('Median is {med}'.format(med=np.median(rolled['Votes'])))
print('Mean is {med}'.format(med=np.mean(rolled['Votes'])))
print('Max is {med}'.format(med=np.max(rolled['Votes'])))
print('Min is {med}'.format(med=np.min(rolled['Votes'])))
# In[ ]: