When updating dataframe column, FractionOfVote, my first step was to add a new column, FractionOfVote, with default NA value. Then parse the dataframe column, Votes, using split.
The following two functions code works fine: 1) add_new_column_fraction(), 2) add_new_column_votes().
def add_new_column_fraction(df):
df['FractionOfVote'] = 'NA'
def add_new_column_votes(df):
df[['YesVotes','NumVotes']] = df['Votes'].str.split('/',expand=True)[[0,1]]
The problem code is found in function calc_fraction_ratio_for_votes()
def calc_fraction_ratio_for_votes(df):
for idx, row in df.iterrows():
numerator = row['YesVotes']
denomerator = row['NumVotes']
try:
row['FractionOfVote'] = float(numerator) / float(denomerator)
except ZeroDivisionError:
row['FractionOfVote'] = 'NaN'
This function takes two other dataframe columns, YesVotes, NumVotes, and calculates a new float value for the new column, FractionOfVote, defined previously in add_new_column_fraction().
The logical error is that column, FractionOfVote, retains the original updated 'NA'; and never received the update from "row['FractionOfVote'] = float(numerator) / float(denomerator)" with either the float value calculation, or the 'NaN' from the "except ZeroDivisionError".