Last active
July 8, 2020 21:52
-
-
Save redbug312/be5702b132ccd5d25503a5ab6d0f66f3 to your computer and use it in GitHub Desktop.
ntuoc13-voting-counts
This file contains hidden or 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
#!/usr/bin/env python3 | |
import pandas as pd | |
import numpy as np | |
def display(df): | |
options = ['display.max_rows', None] | |
options += ['display.unicode.east_asian_width', True] | |
with pd.option_context(*options): | |
print(df) | |
def evaluate(row): | |
return '學號查無此人' if row.team_y is np.nan else \ | |
'大隊號碼錯誤' if row.team_x != row.team_y else \ | |
'投給所屬大隊' if row.team_x in row.votes else '' | |
students = pd.read_csv('students.csv', dtype='str') # columns = [team, id] | |
ballots = pd.read_csv('ballots.csv', dtype='str') # columns = [team, id, votes] | |
students.id = students.id.apply(lambda s: s.strip().upper()) | |
ballots.id = ballots.id.apply(lambda s: s.strip().upper()) | |
ballots.votes = ballots.votes.apply(lambda s: s.split(', ')) | |
leftjoin = ballots.merge(students, left_on='id', right_on='id', how='left', validate='m:1') | |
leftjoin = leftjoin.assign(valid=False, brief='') | |
leftjoin['brief'] = leftjoin.apply(evaluate, axis='columns') | |
dupli = leftjoin.duplicated(['id', 'brief'], keep='last') | |
valid = leftjoin.brief == '' | |
leftjoin.brief[dupli & valid] = '有更新的回覆' | |
leftjoin['valid'] = leftjoin.brief == '' | |
polling_station = leftjoin.votes[leftjoin.valid].explode() | |
result = polling_station.value_counts() | |
display(result) | |
# leftjoin.to_csv('leftjoin.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment