New columns

Data Manipulation with pandas

Richie Cotton

Data Evangelist at DataCamp

Adding a new column

dogs["height_m"] = dogs["height_cm"] / 100

print(dogs)
      name        breed  color  height_cm  weight_kg date_of_birth  height_m
0    Bella     Labrador  Brown         56         24    2013-07-01      0.56
1  Charlie       Poodle  Black         43         24    2016-09-16      0.43
2     Lucy    Chow Chow  Brown         46         24    2014-08-25      0.46
3   Cooper    Schnauzer   Gray         49         17    2011-12-11      0.49
4      Max     Labrador  Black         59         29    2017-01-20      0.59
5   Stella    Chihuahua    Tan         18          2    2015-04-20      0.18
6   Bernie  St. Bernard  White         77         74    2018-02-27      0.77
Data Manipulation with pandas

Doggy mass index

$$\text{BMI} = \text{weight in kg} / \text{(height in m)}^2$$

dogs["bmi"] = dogs["weight_kg"] / dogs["height_m"] ** 2
print(dogs.head())
      name      breed  color  height_cm  weight_kg date_of_birth  height_m         bmi
0    Bella   Labrador  Brown         56         24    2013-07-01      0.56   76.530612
1  Charlie     Poodle  Black         43         24    2016-09-16      0.43  129.799892
2     Lucy  Chow Chow  Brown         46         24    2014-08-25      0.46  113.421550
3   Cooper  Schnauzer   Gray         49         17    2011-12-11      0.49   70.803832
4      Max   Labrador  Black         59         29    2017-01-20      0.59   83.309394
Data Manipulation with pandas

Multiple manipulations

bmi_lt_100 = dogs[dogs["bmi"] < 100]

bmi_lt_100_height = bmi_lt_100.sort_values("height_cm", ascending=False)
bmi_lt_100_height[["name", "height_cm", "bmi"]]
     name  height_cm        bmi
4     Max         59  83.309394
0   Bella         56  76.530612
3  Cooper         49  70.803832
5  Stella         18  61.728395
Data Manipulation with pandas

Let's practice!

Data Manipulation with pandas

Preparing Video For Download...