I'm writing a script in Python and Pandas that uses lambda statements to write pre-formatted comments in a csv column based on the numerical grade assigned to each row. While I'm able to do this for a number of series, I'm having difficulty with one case.
Here the is structure of the csv:
Here is the working code to write a new column composition_comment. (I'm sure there's a way to express this more concisely, but I'm still learning Python and Pandas.)
import pandas as pd
df = pd.read_csv('stack.csv')
composition_score_value = 40 #calculated by another process
composition_comment_a_level = "Good work." # For scores falling between 100 and 90 percent of composition_score_value.
composition_comment_b_level = "Satisfactory work." # For scores between 89 and 80.
composition_comment_c_level = "Improvement needed." # For scores between 79 and 70.
composition_comment_d_level = "Unsatisfactory work." # For scores below 69.
df['composition_comment'] = df['composition_score'].apply(lambda element: composition_comment_a_level if element <= (composition_score_value * 1) else element >= (composition_score_value *.90))
df['composition_comment'] = df['composition_score'].apply(lambda element: composition_comment_b_level if element <= (composition_score_value *.899) else element >= (composition_score_value *.80))
df['composition_comment'] = df['composition_score'].apply(lambda element: composition_comment_c_level if element <= (composition_score_value *.799) else element >= (composition_score_value *.70))
df['composition_comment'] = df['composition_score'].apply(lambda element: composition_comment_d_level if element <= (composition_score_value *.699) else element >= (composition_score_value *.001))
df
df.to_csv('stack.csv', index=False)
The expected output is:
But the actual output is:
Any ideas on why the True values being written, and why the final row processes properly? Any assistance appreciated.


