Pandas: How can I add new rows of data based on conditional statements?

Question:

I am trying to use conditional statements to append rows of data to columns in Pandas. For an example, I have 2 DataFrames: Average Weekly Scores and Grade For Week. Both of these have columns for student names and every week, starting from week1 onwards. I want to take the average score from the week of each student and insert a grade for the week in the Grade For Week DataFrame.

Average Weekly Scores:

df = pd.DataFrame.from_records(
    [
        {"Student": "Samantha", "AVG WK A": 104, "AVG WK B": 114},
        {"Student": "Billy", "AVG WK A": 70, "AVG WK B": 92},
    ],
)

print(df)
+----+-----------+------------+------------+
|    | Student   |   AVG WK A |   AVG WK B |
|----+-----------+------------+------------|
|  0 | Samantha  |        104 |        114 |
|  1 | Billy     |         70 |         92 |
+----+-----------+------------+------------+

Grade For Week (what I want to happen):

df = pd.DataFrame.from_records(
    [
        {"Student": "Samantha", "Week A": "A", "Week B": "A+"},
        {"Student": "Billy", "Week A": "C-", "Week B": "A"},
    ],
)

print(df)
+----+-----------+----------+----------+
|    | Student   | Week A   | Week B   |
|----+-----------+----------+----------|
|  0 | Samantha  | A        | A+       |
|  1 | Billy     | C-       | A        |
+----+-----------+----------+----------+

Essentially, I have a DataFrame with values that I will use for my conditional statements in my second DataFrame. Scores at or over 110 will be an A+, Scores < 110 but > 91 will be A’s and anything less than 91 is a C-. I want to create a for loop that will check every column’s row of data from the Average Weekly Scores DataFrame and give the appropriate grade to the column rows in the Grade For Week DataFrame.

The closest I got to was with the df.loc[len(df)] function. But obviously it added the same values for all the columns like this:

Grade For Week (not what I want to happen):

df = pd.DataFrame.from_records(
    [
        {"Student": "Samantha", "Week A": "A", "Week B": "A"},
        {"Student": "Billy", "Week A": "C-", "Week B": "C-"},
    ],
)

print(df)
+----+-----------+----------+----------+
|    | Student   | Week A   | Week B   |
|----+-----------+----------+----------|
|  0 | Samantha  | A        | A        |
|  1 | Billy     | C-       | C-       |
+----+-----------+----------+----------+

I have been at this for a couple of days now and I could really use some help! Thank you and if you have any other questions please let me know.

Asked By: Nathan Pared

||

Answers:

Lets use np.select to assign the grades based on the range conditions

scores = df.set_index('Student')
scores.columns = scores.columns.str.replace('AVG WK', 'Week')

scores[:] = np.select(
    condlist=[scores >= 110, (scores < 110) & (scores > 91)], 
    choicelist=['A+', 'A'], default='C-'
)

scores = scores.reset_index()

Result

Student Week A Week B
Samantha A A+
Billy C- A
Answered By: Shubham Sharma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.