Skip to content

Instantly share code, notes, and snippets.

View rrosasl's full-sized avatar
💭
Working on Data Science Nanodegree - Udacity

Ricardo Rosas rrosasl

💭
Working on Data Science Nanodegree - Udacity
View GitHub Profile
@rrosasl
rrosasl / importing_g_sheet.py
Last active November 25, 2020 17:11
Importing google sheet
auth.authenticate_user()
# https://colab.research.google.com/notebooks/snippets/sheets.ipynb
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
#worksheet = gc.open('Your spreadsheet name').sheet1
wb = gc.open_by_url('https://docs.google.com/spreadsheets/d/1cH8SE6ba4LqYA0kIS8n9LFt2zJQvh7EZaldcJ_MzgyA/edit#gid=48937339')
# get_all_values gives a list of rows.
wb = wb.worksheet('Form Responses 1')
rows = wb.get_all_values()
@rrosasl
rrosasl / cleaning_g_sheet.py
Created November 25, 2020 17:10
Cleaning spreadsheet data
# Convert Rows into DataFrame and clean data
new_header = df.iloc[0] #grab the first row for the header
df = df[1:] #take the data less the header row
df.columns = new_header #set the header row as the df header
df = df.iloc[:,1:] # Remove time stamp
# Convert to a DataFrame and render.
df = pd.DataFrame.from_records(rows)
new_header = df.iloc[0] #grab the first row for the header
@rrosasl
rrosasl / first_round.py
Created November 25, 2020 17:19
FIrst round of voting
#Create Data Frame where results will be stored
results = []
vote_rounds = pd.DataFrame()
df_t = df.transpose() # Change rows and columns to have voters as columns
for col in df_t.columns:
top_choice = df_t[col].min() #Choose their Top Canidate
top_candidate = df_t[df_t[col] == top_choice].index.tolist()[0]
results.append(top_candidate)
@rrosasl
rrosasl / selecting_winner.py
Created November 25, 2020 17:58
Selecting winner
left_voters = []
losers = []
for r in range(1,df.shape[1]-1):
#Stop loop when there are already two candidates
if vote_rounds[r-1].nunique() == 2:
break
#Start the new voting round
vote_rounds[r] = vote_rounds[r-1]
@rrosasl
rrosasl / Sankey.py
Created November 25, 2020 18:05
SankeyDiagram
def genSankey(df,cat_cols=[],value_cols='',title='Sankey Diagram'):
'''
https://gist.github.com/ken333135/09f8793fff5a6df28558b17e516f91ab
'''
# maximum of 6 value cols -> 6 colors
colorPalette = ['#4B8BBE','#306998','#FFE873','#FFD43B','#646464']
labelList = []
colorNumList = []