If Then
Sat 17 May 2025
title: "Template" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false source: http://pandas.pydata.org/pandas-docs/version/0.24/user_guide/cookbook.html#idioms
import numpy as np
import pandas as pd
df = pd.read_csv('abc.csv')
df
| student | language | science | maths | history | |
|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 |
| 1 | kevin | 10 | 34 | 32 | 67 |
| 2 | sammy | 90 | 23 | 12 | 32 |
| 3 | janice | 20 | 67 | 90 | 45 |
| 4 | peter | 30 | 56 | 45 | 65 |
| 5 | prem | 90 | 45 | 45 | 34 |
| 6 | carrol | 50 | 90 | 45 | 23 |
# someone's math score is 50+, give 20 more marks in their history subject as well
df.loc[df['maths'] > 50, 'history'] += 20
df
| student | language | science | maths | history | |
|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 |
| 1 | kevin | 10 | 34 | 32 | 67 |
| 2 | sammy | 90 | 23 | 12 | 32 |
| 3 | janice | 20 | 67 | 90 | 65 |
| 4 | peter | 30 | 56 | 45 | 65 |
| 5 | prem | 90 | 45 | 45 | 34 |
| 6 | carrol | 50 | 90 | 45 | 23 |
df.loc[df['maths'] > 40, ['history', 'science']] += 10
df
| student | language | science | maths | history | |
|---|---|---|---|---|---|
| 0 | kumar | 90 | 56 | 34 | 34 |
| 1 | kevin | 10 | 34 | 32 | 67 |
| 2 | sammy | 90 | 23 | 12 | 32 |
| 3 | janice | 20 | 77 | 90 | 75 |
| 4 | peter | 30 | 66 | 45 | 75 |
| 5 | prem | 90 | 55 | 45 | 44 |
| 6 | carrol | 50 | 100 | 45 | 33 |
Score: 10
Category: data-wrangling