I am inserting some values into a pandas.DataFrame, I have 4 columns : columns=['activityId', 'completed', 'started', 'completionRate']
The id is a string, completed and started are integers and completionRate is a float (completed/started, is started != 0).
activities_completion_rate = pd.DataFrame(columns=['activityId', 'completed', 'started', 'completionRate'])
activities_completion_rate['completionRate'] = activities_completion_rate['completionRate'].astype(float)
if total_activity_started > 0:
completion_rate = float(total_activity_completed) / total_activity_started
activities_completion_rate = pd.concat([activities_completion_rate, pd.DataFrame(
[(activity_id, total_activity_completed, total_activity_started,
completion_rate)],
columns=['activityId', 'completed', 'started', 'completionRate'])])
print(activity_id, total_activity_completed, total_activity_started, completion_rate)
However the completionRate is always 1 or 0 in the DataFrame, even though the printed value I see in the console (or in the debugger) is a decimal number (ie : 0.6428571428571429 will be converted to 1, 0.5 to 0, so I guess every number strictly above 0.5 is converted to 1 and under to 0)
As you can see I tried to “force convert” the values to float before doing the completionRate calculation, I also tried to put the new line into a new dataframe before the concat and force completionRate to be a float in the DataFrame (even though it was already a float according to the debugger):
if total_activity_started > 0:
completion_rate = float(total_activity_completed) / total_activity_started
a =pd.DataFrame(
[(activity_id, total_activity_completed, total_activity_started,
float(completion_rate))],
columns=['activityId', 'completed', 'started', 'completionRate'])
activities_completion_rate = pd.concat([activities_completion_rate, pd.DataFrame(
[(activity_id, total_activity_completed, total_activity_started,
completion_rate)],
columns=['activityId', 'completed', 'started', 'completionRate'])])
print(activity_id, total_activity_completed, total_activity_started, completion_rate)
The column type of ‘completionRate’ in DataFrame a is float64
I never had such issue before so I really don’t know what else I can do