I have a table like this:
| id | q1_score | q2_score | q3_score | q4_score | quarter |
|---|---|---|---|---|---|
| 1 | -0.77 | -0.55 | 0.21 | 3.42 | q1 |
| 2 | -0.77 | -0.55 | 0.21 | 3.42 | q1 |
| 3 | -0.77 | -0.55 | 0.21 | 3.42 | q2 |
| 4 | -0.77 | -0.55 | 0.21 | 3.42 | q3 |
| 5 | -0.77 | -0.55 | 0.21 | 3.42 | q4 |
I want to create a new column quarter_score based on the value of quarter.
- If
quarter==q1thenquarter_score=value fromq1_scoreof that row. - If
quarter==q2thenquarter_score=value fromq2_scoreof that row. - so forth ...
My code looks like this
mapper = {'q1': df['q1_score'], 'q2':df['q2_score'], 'q3': df['q3_score'], 'q4': df['q4_score']}
df['quarter_score'] = df['quarter'].map(mapper)
but it does not take single value, it takes whole rows for that column for each row.
| id | q1_score | q2_score | q3_score | q4_score | quarter | quarter_score |
|---|---|---|---|---|---|---|
| 1 | -0.77 | -0.55 | 0.21 | 3.42 | q1 | 0 -0.77 1 -0.77 2 -0.77 |
| 2 | -0.77 | -0.55 | 0.21 | 3.42 | q1 | 0 -0.77 1 -0.77 2 -0.77 |
| 3 | -0.77 | -0.55 | 0.21 | 3.42 | q2 | 0 -0.55 1 -0.55 2 -0.55 |
| 4 | -0.77 | -0.55 | 0.21 | 3.42 | q3 | 0 0.21 1 0.21 2 0.21 |
| 5 | -0.77 | -0.55 | 0.21 | 3.42 | q4 | 0 3.42 1 3.42 2 3.42 |
My final output should be like this below.
| id | q1_score | q2_score | q3_score | q4_score | quarter | quarter_score |
|---|---|---|---|---|---|---|
| 1 | -0.77 | -0.55 | 0.21 | 3.42 | q1 | -0.77 |
| 2 | -0.77 | -0.55 | 0.21 | 3.42 | q1 | -0.77 |
| 3 | -0.77 | -0.55 | 0.21 | 3.42 | q2 | -0.55 |
| 4 | -0.77 | -0.55 | 0.21 | 3.42 | q3 | 0.21 |
| 5 | -0.77 | -0.55 | 0.21 | 3.42 | q4 | 3.42 |