Change-Columns-Based-On-Other-Column
Sat 17 May 2025
title: "Change Column Based on Other Column" author: "Rj" date: 2019-04-24 description: "-" type: technical_note draft: false
import numpy as np
import pandas as pd
df = pd.read_csv('marks.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 |
# Whoever got more than 40 marks in Maths, we will double their marks
df.loc[df.maths > 40, 'history'] = 2 * df['history']
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 | 90 |
| 4 | peter | 30 | 56 | 45 | 130 |
| 5 | prem | 90 | 45 | 45 | 68 |
| 6 | carrol | 50 | 90 | 45 | 46 |
Score: 5
Category: data-wrangling