Skip to content

Instantly share code, notes, and snippets.

@cab938
Last active April 21, 2018 17:22
Show Gist options
  • Save cab938/f84d8429dd58c106cd6d54b072ce9851 to your computer and use it in GitHub Desktop.
Save cab938/f84d8429dd58c106cd6d54b072ce9851 to your computer and use it in GitHub Desktop.
#!pip install html5lib #install html5lib, only needs to be run once
#You might need to restart kernel after running with the menu Kernel>Restart
import pandas as pd
import numpy as np
df_power=pd.read_csv('https://proxy.mentoracademy.org/getContentFromUrl/?userid=brooks&url=https%3A%2F%2Fgist.github.com%2Fcab938%2F71e8371ebc621a105afa2181efd78e75%2Fraw%2Ffafe9712373ab5a1d3b2fdb6ac09a28cbcfe8f82%2Fus_power.csv')
df_states=pd.read_csv('https://proxy.mentoracademy.org/getContentFromUrl/?userid=brooks&url=https%3A%2F%2Fgist.github.com%2Fcab938%2Ffaedc9046a01b2170c0b252fbc4fc416%2Fraw%2Ff5fa5974e7fef6b996e8ff8583f8d5b47ce391c5%2Fus_states.csv')
joined_df=pd.merge(df_states, df_power, left_on=["state"], right_on=["state"], how="inner") #join frames and only consider places we have data for both the state pop and renewables
def pct_renw_by_pop(x):
#print(x['renewables in GWh'])
return float(x['renewables in GWh'])/float(x['population'])
joined_df['renw_by_pop']= joined_df.apply(pct_renw_by_pop ,axis=1) #apply the function above to create a new column
joined_df['renw_by_pop_rank']= joined_df['renw_by_pop'].rank(ascending=False) #determine the ranks for each state in a new column
joined_df.sort_values(by='renw_by_pop_rank').head(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment