This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |