Exercise 3.3: Adding data to a data frame
[1]:
import pandas as pd
We continue working with the frog tongue data. Recall that the header comments in the data file contained information about the frogs.
[2]:
!head -20 data/frog_tongue_adhesion.csv
# These data are from the paper,
# Kleinteich and Gorb, Sci. Rep., 4, 5225, 2014.
# It was featured in the New York Times.
# http://www.nytimes.com/2014/08/25/science/a-frog-thats-a-living-breathing-pac-man.html
#
# The authors included the data in their supplemental information.
#
# Importantly, the ID refers to the identifites of the frogs they tested.
# I: adult, 63 mm snout-vent-length (SVL) and 63.1 g body weight,
# Ceratophrys cranwelli crossed with Ceratophrys cornuta
# II: adult, 70 mm SVL and 72.7 g body weight,
# Ceratophrys cranwelli crossed with Ceratophrys cornuta
# III: juvenile, 28 mm SVL and 12.7 g body weight, Ceratophrys cranwelli
# IV: juvenile, 31 mm SVL and 12.7 g body weight, Ceratophrys cranwelli
date,ID,trial number,impact force (mN),impact time (ms),impact force / body weight,adhesive force (mN),time frog pulls on target (ms),adhesive force / body weight,adhesive impulse (N-s),total contact area (mm2),contact area without mucus (mm2),contact area with mucus / contact area without mucus,contact pressure (Pa),adhesive strength (Pa)
2013_02_26,I,3,1205,46,1.95,-785,884,1.27,-0.290,387,70,0.82,3117,-2030
2013_02_26,I,4,2527,44,4.08,-983,248,1.59,-0.181,101,94,0.07,24923,-9695
2013_03_01,I,1,1745,34,2.82,-850,211,1.37,-0.157,83,79,0.05,21020,-10239
2013_03_01,I,2,1556,41,2.51,-455,1025,0.74,-0.170,330,158,0.52,4718,-1381
2013_03_01,I,3,493,36,0.80,-974,499,1.57,-0.423,245,216,0.12,2012,-3975
So, each frog has associated with it an age (adult or juvenile), snout-vent-length (SVL), body weight, and species (either cross or cranwelli). For a tidy data frame, we should have a column for each of these values. Your task is to load in the data, and then add these columns to the data frame. For convenience, here is a data frame with data about each frog.
[3]:
df_frog = pd.DataFrame(
data={
"ID": ["I", "II", "III", "IV"],
"age": ["adult", "adult", "juvenile", "juvenile"],
"SVL (mm)": [63, 70, 28, 31],
"weight (g)": [63.1, 72.7, 12.7, 12.7],
"species": ["cross", "cross", "cranwelli", "cranwelli"],
}
)
Note: There are lots of ways to solve this problem. This is a good exercise in searching through the Pandas documentation and other online resources, such as Stack Overflow. Remember, much of your programming efforts are spent searching through documentation and the internet.
Finally, as a fun challenge, see if you can highlight the strike with the highest impact force for each frog in the data frame.
Solution
The most direct way is to use built-in pd.merge() function. This function finds a common column between two DataFrames
, and then uses that column to merge them, filling in values that match in the common column. This is exactly what we want.
[4]:
# Load the data
df = pd.read_csv('data/frog_tongue_adhesion.csv', comment='#')
# Perform merge
df = df.merge(df_frog)
Let’s look at the DataFrame
to make sure it has what we expect.
[5]:
df.head()
[5]:
date | ID | trial number | impact force (mN) | impact time (ms) | impact force / body weight | adhesive force (mN) | time frog pulls on target (ms) | adhesive force / body weight | adhesive impulse (N-s) | total contact area (mm2) | contact area without mucus (mm2) | contact area with mucus / contact area without mucus | contact pressure (Pa) | adhesive strength (Pa) | age | SVL (mm) | weight (g) | species | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2013_02_26 | I | 3 | 1205 | 46 | 1.95 | -785 | 884 | 1.27 | -0.290 | 387 | 70 | 0.82 | 3117 | -2030 | adult | 63 | 63.1 | cross |
1 | 2013_02_26 | I | 4 | 2527 | 44 | 4.08 | -983 | 248 | 1.59 | -0.181 | 101 | 94 | 0.07 | 24923 | -9695 | adult | 63 | 63.1 | cross |
2 | 2013_03_01 | I | 1 | 1745 | 34 | 2.82 | -850 | 211 | 1.37 | -0.157 | 83 | 79 | 0.05 | 21020 | -10239 | adult | 63 | 63.1 | cross |
3 | 2013_03_01 | I | 2 | 1556 | 41 | 2.51 | -455 | 1025 | 0.74 | -0.170 | 330 | 158 | 0.52 | 4718 | -1381 | adult | 63 | 63.1 | cross |
4 | 2013_03_01 | I | 3 | 493 | 36 | 0.80 | -974 | 499 | 1.57 | -0.423 | 245 | 216 | 0.12 | 2012 | -3975 | adult | 63 | 63.1 | cross |
Now, we can highlight the strike with the highest impact force for each frog. One way to do this is to write a function that returns an entire data frame with the same index and columns as df
where each entry contains a formatting string like 'background-color: lightgray'
, and then use df.style.apply()
with the axis=None
kwarg.
[6]:
def highlight_max(df):
# Make full data frame with all strings being light gray bg color
highlight_df = pd.DataFrame(
"background-color: lightgray", index=df.index, columns=df.columns
)
# Which rows contain a maximum force
has_max_force = (
df.groupby("ID")["impact force (mN)"]
.transform("max")
.eq(df["impact force (mN)"])
)
# Color rows with maximum force green
highlight_df.loc[has_max_force, :] = 'background-color: #7fc97f'
return highlight_df
df.style.apply(highlight_max, axis=None)
[6]:
date | ID | trial number | impact force (mN) | impact time (ms) | impact force / body weight | adhesive force (mN) | time frog pulls on target (ms) | adhesive force / body weight | adhesive impulse (N-s) | total contact area (mm2) | contact area without mucus (mm2) | contact area with mucus / contact area without mucus | contact pressure (Pa) | adhesive strength (Pa) | age | SVL (mm) | weight (g) | species | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2013_02_26 | I | 3 | 1205 | 46 | 1.950000 | -785 | 884 | 1.270000 | -0.290000 | 387 | 70 | 0.820000 | 3117 | -2030 | adult | 63 | 63.100000 | cross |
1 | 2013_02_26 | I | 4 | 2527 | 44 | 4.080000 | -983 | 248 | 1.590000 | -0.181000 | 101 | 94 | 0.070000 | 24923 | -9695 | adult | 63 | 63.100000 | cross |
2 | 2013_03_01 | I | 1 | 1745 | 34 | 2.820000 | -850 | 211 | 1.370000 | -0.157000 | 83 | 79 | 0.050000 | 21020 | -10239 | adult | 63 | 63.100000 | cross |
3 | 2013_03_01 | I | 2 | 1556 | 41 | 2.510000 | -455 | 1025 | 0.740000 | -0.170000 | 330 | 158 | 0.520000 | 4718 | -1381 | adult | 63 | 63.100000 | cross |
4 | 2013_03_01 | I | 3 | 493 | 36 | 0.800000 | -974 | 499 | 1.570000 | -0.423000 | 245 | 216 | 0.120000 | 2012 | -3975 | adult | 63 | 63.100000 | cross |
5 | 2013_03_01 | I | 4 | 2276 | 31 | 3.680000 | -592 | 969 | 0.960000 | -0.176000 | 341 | 106 | 0.690000 | 6676 | -1737 | adult | 63 | 63.100000 | cross |
6 | 2013_03_05 | I | 1 | 556 | 43 | 0.900000 | -512 | 835 | 0.830000 | -0.285000 | 359 | 110 | 0.690000 | 1550 | -1427 | adult | 63 | 63.100000 | cross |
7 | 2013_03_05 | I | 2 | 1928 | 46 | 3.110000 | -804 | 508 | 1.300000 | -0.285000 | 246 | 178 | 0.280000 | 7832 | -3266 | adult | 63 | 63.100000 | cross |
8 | 2013_03_05 | I | 3 | 2641 | 50 | 4.270000 | -690 | 491 | 1.120000 | -0.239000 | 269 | 224 | 0.170000 | 9824 | -2568 | adult | 63 | 63.100000 | cross |
9 | 2013_03_05 | I | 4 | 1897 | 41 | 3.060000 | -462 | 839 | 0.750000 | -0.328000 | 266 | 176 | 0.340000 | 7122 | -1733 | adult | 63 | 63.100000 | cross |
10 | 2013_03_12 | I | 1 | 1891 | 40 | 3.060000 | -766 | 1069 | 1.240000 | -0.380000 | 408 | 33 | 0.920000 | 4638 | -1879 | adult | 63 | 63.100000 | cross |
11 | 2013_03_12 | I | 2 | 1545 | 48 | 2.500000 | -715 | 649 | 1.150000 | -0.298000 | 141 | 112 | 0.210000 | 10947 | -5064 | adult | 63 | 63.100000 | cross |
12 | 2013_03_12 | I | 3 | 1307 | 29 | 2.110000 | -613 | 1845 | 0.990000 | -0.768000 | 455 | 92 | 0.800000 | 2874 | -1348 | adult | 63 | 63.100000 | cross |
13 | 2013_03_12 | I | 4 | 1692 | 31 | 2.730000 | -677 | 917 | 1.090000 | -0.457000 | 186 | 129 | 0.310000 | 9089 | -3636 | adult | 63 | 63.100000 | cross |
14 | 2013_03_12 | I | 5 | 1543 | 38 | 2.490000 | -528 | 750 | 0.850000 | -0.353000 | 153 | 148 | 0.030000 | 10095 | -3453 | adult | 63 | 63.100000 | cross |
15 | 2013_03_15 | I | 1 | 1282 | 31 | 2.070000 | -452 | 785 | 0.730000 | -0.253000 | 290 | 105 | 0.640000 | 4419 | -1557 | adult | 63 | 63.100000 | cross |
16 | 2013_03_15 | I | 2 | 775 | 34 | 1.250000 | -430 | 837 | 0.700000 | -0.276000 | 257 | 124 | 0.520000 | 3019 | -1677 | adult | 63 | 63.100000 | cross |
17 | 2013_03_15 | I | 3 | 2032 | 60 | 3.280000 | -652 | 486 | 1.050000 | -0.257000 | 147 | 134 | 0.090000 | 13784 | -4425 | adult | 63 | 63.100000 | cross |
18 | 2013_03_15 | I | 4 | 1240 | 34 | 2.000000 | -692 | 906 | 1.120000 | -0.317000 | 364 | 260 | 0.280000 | 3406 | -1901 | adult | 63 | 63.100000 | cross |
19 | 2013_03_15 | I | 5 | 473 | 40 | 0.760000 | -536 | 1218 | 0.870000 | -0.382000 | 259 | 168 | 0.350000 | 1830 | -2073 | adult | 63 | 63.100000 | cross |
20 | 2013_03_19 | II | 1 | 1612 | 18 | 3.790000 | -655 | 3087 | 1.540000 | -0.385000 | 348 | 15 | 0.960000 | 4633 | -1881 | adult | 70 | 72.700000 | cross |
21 | 2013_03_19 | II | 2 | 605 | 55 | 1.420000 | -292 | 1261 | 0.690000 | -0.294000 | 248 | 24 | 0.900000 | 2441 | -1177 | adult | 70 | 72.700000 | cross |
22 | 2013_03_19 | II | 3 | 327 | 51 | 0.770000 | -246 | 1508 | 0.580000 | -0.282000 | 130 | 34 | 0.740000 | 2517 | -1894 | adult | 70 | 72.700000 | cross |
23 | 2013_03_19 | II | 4 | 946 | 59 | 2.230000 | -245 | 1841 | 0.580000 | -0.340000 | 106 | 26 | 0.760000 | 8893 | -2301 | adult | 70 | 72.700000 | cross |
24 | 2013_03_21 | II | 1 | 541 | 33 | 1.270000 | -553 | 3126 | 1.300000 | -0.432000 | 276 | 16 | 0.940000 | 1959 | -2004 | adult | 70 | 72.700000 | cross |
25 | 2013_03_21 | II | 2 | 1539 | 43 | 3.620000 | -664 | 741 | 1.560000 | -0.046000 | 85 | 24 | 0.720000 | 18073 | -7802 | adult | 70 | 72.700000 | cross |
26 | 2013_03_21 | II | 3 | 529 | 28 | 1.240000 | -261 | 2482 | 0.610000 | -0.414000 | 325 | 33 | 0.900000 | 1627 | -803 | adult | 70 | 72.700000 | cross |
27 | 2013_03_21 | II | 4 | 628 | 31 | 1.480000 | -691 | 998 | 1.630000 | -0.071000 | 242 | 67 | 0.720000 | 2600 | -2860 | adult | 70 | 72.700000 | cross |
28 | 2013_03_25 | II | 1 | 1453 | 72 | 3.420000 | -92 | 1652 | 0.220000 | -0.008000 | 136 | 0 | 1.000000 | 10645 | -678 | adult | 70 | 72.700000 | cross |
29 | 2013_03_25 | II | 2 | 297 | 42 | 0.700000 | -566 | 936 | 1.330000 | -0.084000 | 126 | 4 | 0.970000 | 2367 | -4506 | adult | 70 | 72.700000 | cross |
30 | 2013_03_25 | II | 3 | 703 | 33 | 1.650000 | -223 | 2152 | 0.520000 | -0.209000 | 237 | 8 | 0.970000 | 2972 | -942 | adult | 70 | 72.700000 | cross |
31 | 2013_03_25 | II | 4 | 269 | 57 | 0.630000 | -512 | 189 | 1.200000 | -0.055000 | 29 | 28 | 0.030000 | 9279 | -17652 | adult | 70 | 72.700000 | cross |
32 | 2013_03_28 | II | 1 | 751 | 39 | 1.770000 | -227 | 1195 | 0.530000 | -0.026000 | 206 | 0 | 1.000000 | 3647 | -1101 | adult | 70 | 72.700000 | cross |
33 | 2013_03_28 | II | 2 | 245 | 21 | 0.580000 | -573 | 1466 | 1.350000 | -0.215000 | 190 | 46 | 0.760000 | 1288 | -3014 | adult | 70 | 72.700000 | cross |
34 | 2013_04_03 | II | 1 | 1182 | 28 | 2.780000 | -522 | 1197 | 1.230000 | -0.118000 | 281 | 0 | 1.000000 | 4213 | -1860 | adult | 70 | 72.700000 | cross |
35 | 2013_04_03 | II | 2 | 515 | 29 | 1.210000 | -599 | 1486 | 1.410000 | -0.226000 | 217 | 0 | 1.000000 | 2369 | -2757 | adult | 70 | 72.700000 | cross |
36 | 2013_04_08 | II | 1 | 435 | 26 | 1.020000 | -364 | 1017 | 0.860000 | -0.211000 | 189 | 89 | 0.530000 | 2302 | -1927 | adult | 70 | 72.700000 | cross |
37 | 2013_04_08 | II | 2 | 383 | 31 | 0.900000 | -469 | 974 | 1.100000 | -0.260000 | 221 | 72 | 0.670000 | 1737 | -2129 | adult | 70 | 72.700000 | cross |
38 | 2013_04_08 | II | 3 | 457 | 15 | 1.080000 | -844 | 780 | 1.990000 | -0.328000 | 171 | 106 | 0.380000 | 2665 | -4925 | adult | 70 | 72.700000 | cross |
39 | 2013_04_12 | II | 1 | 730 | 42 | 1.720000 | -648 | 786 | 1.520000 | -0.121000 | 142 | 43 | 0.700000 | 5149 | -4573 | adult | 70 | 72.700000 | cross |
40 | 2013_05_27 | III | 1 | 614 | 88 | 4.940000 | -94 | 683 | 0.760000 | -0.001000 | 97 | 15 | 0.830000 | 6326 | -967 | juvenile | 28 | 12.700000 | cranwelli |
41 | 2013_05_27 | III | 2 | 414 | 143 | 3.330000 | -163 | 245 | 1.310000 | -0.032000 | 108 | 10 | 0.600000 | 3824 | -1507 | juvenile | 28 | 12.700000 | cranwelli |
42 | 2013_05_27 | III | 3 | 324 | 105 | 2.610000 | -172 | 619 | 1.380000 | -0.079000 | 55 | 23 | 0.370000 | 5946 | -3149 | juvenile | 28 | 12.700000 | cranwelli |
43 | 2013_06_11 | III | 1 | 776 | 35 | 6.240000 | -225 | 1823 | 1.810000 | -0.132000 | 124 | 17 | 0.770000 | 6272 | -1818 | juvenile | 28 | 12.700000 | cranwelli |
44 | 2013_06_11 | III | 2 | 611 | 29 | 4.910000 | -301 | 918 | 2.420000 | -0.155000 | 128 | 43 | 0.020000 | 4770 | -2354 | juvenile | 28 | 12.700000 | cranwelli |
45 | 2013_06_11 | III | 3 | 544 | 16 | 4.380000 | -93 | 1351 | 0.750000 | -0.110000 | 43 | 34 | 0.710000 | 12699 | -2181 | juvenile | 28 | 12.700000 | cranwelli |
46 | 2013_06_14 | III | 1 | 538 | 38 | 4.320000 | -131 | 1790 | 1.050000 | -0.036000 | 130 | 74 | 1.000000 | 4130 | -1005 | juvenile | 28 | 12.700000 | cranwelli |
47 | 2013_06_14 | III | 2 | 579 | 31 | 4.660000 | -289 | 1006 | 2.330000 | -0.073000 | 113 | 4 | 0.480000 | 5110 | -2555 | juvenile | 28 | 12.700000 | cranwelli |
48 | 2013_06_18 | III | 1 | 806 | 29 | 6.490000 | -104 | 883 | 0.840000 | -0.055000 | 115 | 55 | 0.660000 | 6993 | -902 | juvenile | 28 | 12.700000 | cranwelli |
49 | 2013_06_18 | III | 2 | 459 | 32 | 3.700000 | -229 | 1218 | 1.850000 | -0.137000 | 89 | 6 | 0.950000 | 5165 | -2580 | juvenile | 28 | 12.700000 | cranwelli |
50 | 2013_06_18 | III | 3 | 458 | 30 | 3.690000 | -259 | 910 | 2.080000 | -0.194000 | 91 | 88 | 0.150000 | 5048 | -2855 | juvenile | 28 | 12.700000 | cranwelli |
51 | 2013_06_18 | III | 4 | 626 | 16 | 5.040000 | -231 | 550 | 1.860000 | -0.042000 | 82 | 23 | 0.010000 | 7633 | -2819 | juvenile | 28 | 12.700000 | cranwelli |
52 | 2013_06_21 | III | 1 | 621 | 27 | 4.990000 | -267 | 2081 | 2.140000 | -0.183000 | 120 | 58 | 0.900000 | 5152 | -2213 | juvenile | 28 | 12.700000 | cranwelli |
53 | 2013_06_21 | III | 2 | 544 | 30 | 4.380000 | -178 | 376 | 1.430000 | -0.034000 | 19 | 17 | 0.050000 | 28641 | -9364 | juvenile | 28 | 12.700000 | cranwelli |
54 | 2013_06_21 | III | 3 | 535 | 35 | 4.300000 | -123 | 289 | 0.990000 | -0.029000 | 21 | 29 | 0.050000 | 25471 | -5843 | juvenile | 28 | 12.700000 | cranwelli |
55 | 2013_06_21 | III | 4 | 385 | 39 | 3.090000 | -151 | 607 | 1.220000 | -0.082000 | 31 | 126 | 0.030000 | 12409 | -4882 | juvenile | 28 | 12.700000 | cranwelli |
56 | 2013_06_26 | III | 1 | 401 | 36 | 3.230000 | -127 | 2932 | 1.020000 | -0.215000 | 142 | 12 | 0.860000 | 2835 | -896 | juvenile | 28 | 12.700000 | cranwelli |
57 | 2013_06_26 | III | 2 | 614 | 34 | 4.940000 | -372 | 680 | 2.990000 | -0.140000 | 72 | 1 | 0.420000 | 8475 | -5136 | juvenile | 28 | 12.700000 | cranwelli |
58 | 2013_06_26 | III | 3 | 665 | 40 | 5.350000 | -236 | 685 | 1.900000 | -0.118000 | 129 | 0 | 0.160000 | 5171 | -1834 | juvenile | 28 | 12.700000 | cranwelli |
59 | 2013_06_26 | III | 4 | 488 | 34 | 3.930000 | -390 | 1308 | 3.140000 | -0.208000 | 112 | 58 | 0.390000 | 4376 | -3492 | juvenile | 28 | 12.700000 | cranwelli |
60 | 2013_05_27 | IV | 2 | 172 | 26 | 1.280000 | -456 | 462 | 3.400000 | -0.050000 | 133 | 0 | 0.880000 | 1297 | -3440 | juvenile | 31 | 12.700000 | cranwelli |
61 | 2013_05_27 | IV | 3 | 142 | 20 | 1.050000 | -193 | 250 | 1.440000 | -0.047000 | 57 | 74 | 0.830000 | 2498 | -3400 | juvenile | 31 | 12.700000 | cranwelli |
62 | 2013_05_27 | IV | 4 | 37 | 55 | 0.280000 | -236 | 743 | 1.760000 | -0.119000 | 51 | 44 | 0.540000 | 735 | -4647 | juvenile | 31 | 12.700000 | cranwelli |
63 | 2013_05_30 | IV | 1 | 453 | 38 | 3.370000 | -225 | 844 | 1.680000 | -0.110000 | 142 | 108 | 0.480000 | 3177 | -1581 | juvenile | 31 | 12.700000 | cranwelli |
64 | 2013_05_30 | IV | 2 | 355 | 31 | 2.640000 | -217 | 728 | 1.610000 | -0.023000 | 174 | 39 | 0.980000 | 2037 | -1245 | juvenile | 31 | 12.700000 | cranwelli |
65 | 2013_05_30 | IV | 3 | 22 | 33 | 0.170000 | -161 | 472 | 1.200000 | -0.052000 | 56 | 4 | 0.020000 | 397 | -2866 | juvenile | 31 | 12.700000 | cranwelli |
66 | 2013_06_03 | IV | 1 | 502 | 74 | 3.740000 | -139 | 959 | 1.040000 | -0.089000 | 65 | 77 | 0.910000 | 7713 | -2141 | juvenile | 31 | 12.700000 | cranwelli |
67 | 2013_06_11 | IV | 1 | 273 | 26 | 2.030000 | -264 | 844 | 1.970000 | -0.160000 | 124 | 81 | 0.290000 | 2205 | -2136 | juvenile | 31 | 12.700000 | cranwelli |
68 | 2013_06_11 | IV | 2 | 720 | 27 | 5.360000 | -342 | 1515 | 2.550000 | -0.226000 | 137 | 0 | 0.830000 | 5259 | -2497 | juvenile | 31 | 12.700000 | cranwelli |
69 | 2013_06_11 | IV | 3 | 582 | 33 | 4.340000 | -231 | 279 | 1.720000 | -0.033000 | 60 | 4 | 0.030000 | 9705 | -3847 | juvenile | 31 | 12.700000 | cranwelli |
70 | 2013_06_11 | IV | 4 | 198 | 23 | 1.470000 | -209 | 1427 | 1.550000 | -0.151000 | 110 | 69 | 0.840000 | 1793 | -1889 | juvenile | 31 | 12.700000 | cranwelli |
71 | 2013_06_14 | IV | 1 | 198 | 6 | 1.470000 | -292 | 2874 | 2.170000 | -0.232000 | 145 | 50 | 0.990000 | 1369 | -2018 | juvenile | 31 | 12.700000 | cranwelli |
72 | 2013_06_18 | IV | 1 | 597 | 29 | 4.440000 | -339 | 4251 | 2.530000 | -0.281000 | 191 | 12 | 1.000000 | 3116 | -1772 | juvenile | 31 | 12.700000 | cranwelli |
73 | 2013_06_18 | IV | 2 | 516 | 31 | 3.840000 | -371 | 626 | 2.760000 | -0.094000 | 83 | 18 | 0.120000 | 6184 | -4447 | juvenile | 31 | 12.700000 | cranwelli |
74 | 2013_06_18 | IV | 3 | 815 | 34 | 6.070000 | -331 | 1254 | 2.470000 | -0.077000 | 151 | 20 | 0.710000 | 5386 | -2190 | juvenile | 31 | 12.700000 | cranwelli |
75 | 2013_06_18 | IV | 4 | 402 | 38 | 3.000000 | -302 | 986 | 2.250000 | -0.122000 | 117 | 30 | 0.070000 | 3446 | -2591 | juvenile | 31 | 12.700000 | cranwelli |
76 | 2013_06_21 | IV | 1 | 605 | 39 | 4.500000 | -216 | 1627 | 1.610000 | -0.139000 | 123 | 20 | 1.000000 | 4928 | -1759 | juvenile | 31 | 12.700000 | cranwelli |
77 | 2013_06_21 | IV | 2 | 711 | 76 | 5.300000 | -163 | 2021 | 1.210000 | -0.217000 | 129 | 42 | 0.970000 | 5498 | -1257 | juvenile | 31 | 12.700000 | cranwelli |
78 | 2013_06_21 | IV | 3 | 614 | 33 | 4.570000 | -367 | 1366 | 2.730000 | -0.198000 | 128 | 108 | 0.460000 | 4776 | -2857 | juvenile | 31 | 12.700000 | cranwelli |
79 | 2013_06_21 | IV | 4 | 468 | 36 | 3.480000 | -218 | 1269 | 1.630000 | -0.122000 | 129 | 68 | 0.610000 | 3617 | -1688 | juvenile | 31 | 12.700000 | cranwelli |
While this works and does not require adding anything to the data frame, I think it is more concise to simply add a column that is a Boolean stating whether or not the row contains a maximum impact force and then using this column to determine how each row is shaded.
[7]:
%load_ext blackcellmagic
[8]:
df = df.assign(
max_strike=df.groupby("ID")["impact force (mN)"]
.transform("max")
.eq(df["impact force (mN)"])
)
def highlight_max(s):
if s["max_strike"]:
return ["background-color: #7fc97f"] * len(s)
else:
return ["background-color: lightgray"] * len(s)
df.style.apply(highlight_max, axis=1)
[8]:
date | ID | trial number | impact force (mN) | impact time (ms) | impact force / body weight | adhesive force (mN) | time frog pulls on target (ms) | adhesive force / body weight | adhesive impulse (N-s) | total contact area (mm2) | contact area without mucus (mm2) | contact area with mucus / contact area without mucus | contact pressure (Pa) | adhesive strength (Pa) | age | SVL (mm) | weight (g) | species | max_strike | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2013_02_26 | I | 3 | 1205 | 46 | 1.950000 | -785 | 884 | 1.270000 | -0.290000 | 387 | 70 | 0.820000 | 3117 | -2030 | adult | 63 | 63.100000 | cross | False |
1 | 2013_02_26 | I | 4 | 2527 | 44 | 4.080000 | -983 | 248 | 1.590000 | -0.181000 | 101 | 94 | 0.070000 | 24923 | -9695 | adult | 63 | 63.100000 | cross | False |
2 | 2013_03_01 | I | 1 | 1745 | 34 | 2.820000 | -850 | 211 | 1.370000 | -0.157000 | 83 | 79 | 0.050000 | 21020 | -10239 | adult | 63 | 63.100000 | cross | False |
3 | 2013_03_01 | I | 2 | 1556 | 41 | 2.510000 | -455 | 1025 | 0.740000 | -0.170000 | 330 | 158 | 0.520000 | 4718 | -1381 | adult | 63 | 63.100000 | cross | False |
4 | 2013_03_01 | I | 3 | 493 | 36 | 0.800000 | -974 | 499 | 1.570000 | -0.423000 | 245 | 216 | 0.120000 | 2012 | -3975 | adult | 63 | 63.100000 | cross | False |
5 | 2013_03_01 | I | 4 | 2276 | 31 | 3.680000 | -592 | 969 | 0.960000 | -0.176000 | 341 | 106 | 0.690000 | 6676 | -1737 | adult | 63 | 63.100000 | cross | False |
6 | 2013_03_05 | I | 1 | 556 | 43 | 0.900000 | -512 | 835 | 0.830000 | -0.285000 | 359 | 110 | 0.690000 | 1550 | -1427 | adult | 63 | 63.100000 | cross | False |
7 | 2013_03_05 | I | 2 | 1928 | 46 | 3.110000 | -804 | 508 | 1.300000 | -0.285000 | 246 | 178 | 0.280000 | 7832 | -3266 | adult | 63 | 63.100000 | cross | False |
8 | 2013_03_05 | I | 3 | 2641 | 50 | 4.270000 | -690 | 491 | 1.120000 | -0.239000 | 269 | 224 | 0.170000 | 9824 | -2568 | adult | 63 | 63.100000 | cross | True |
9 | 2013_03_05 | I | 4 | 1897 | 41 | 3.060000 | -462 | 839 | 0.750000 | -0.328000 | 266 | 176 | 0.340000 | 7122 | -1733 | adult | 63 | 63.100000 | cross | False |
10 | 2013_03_12 | I | 1 | 1891 | 40 | 3.060000 | -766 | 1069 | 1.240000 | -0.380000 | 408 | 33 | 0.920000 | 4638 | -1879 | adult | 63 | 63.100000 | cross | False |
11 | 2013_03_12 | I | 2 | 1545 | 48 | 2.500000 | -715 | 649 | 1.150000 | -0.298000 | 141 | 112 | 0.210000 | 10947 | -5064 | adult | 63 | 63.100000 | cross | False |
12 | 2013_03_12 | I | 3 | 1307 | 29 | 2.110000 | -613 | 1845 | 0.990000 | -0.768000 | 455 | 92 | 0.800000 | 2874 | -1348 | adult | 63 | 63.100000 | cross | False |
13 | 2013_03_12 | I | 4 | 1692 | 31 | 2.730000 | -677 | 917 | 1.090000 | -0.457000 | 186 | 129 | 0.310000 | 9089 | -3636 | adult | 63 | 63.100000 | cross | False |
14 | 2013_03_12 | I | 5 | 1543 | 38 | 2.490000 | -528 | 750 | 0.850000 | -0.353000 | 153 | 148 | 0.030000 | 10095 | -3453 | adult | 63 | 63.100000 | cross | False |
15 | 2013_03_15 | I | 1 | 1282 | 31 | 2.070000 | -452 | 785 | 0.730000 | -0.253000 | 290 | 105 | 0.640000 | 4419 | -1557 | adult | 63 | 63.100000 | cross | False |
16 | 2013_03_15 | I | 2 | 775 | 34 | 1.250000 | -430 | 837 | 0.700000 | -0.276000 | 257 | 124 | 0.520000 | 3019 | -1677 | adult | 63 | 63.100000 | cross | False |
17 | 2013_03_15 | I | 3 | 2032 | 60 | 3.280000 | -652 | 486 | 1.050000 | -0.257000 | 147 | 134 | 0.090000 | 13784 | -4425 | adult | 63 | 63.100000 | cross | False |
18 | 2013_03_15 | I | 4 | 1240 | 34 | 2.000000 | -692 | 906 | 1.120000 | -0.317000 | 364 | 260 | 0.280000 | 3406 | -1901 | adult | 63 | 63.100000 | cross | False |
19 | 2013_03_15 | I | 5 | 473 | 40 | 0.760000 | -536 | 1218 | 0.870000 | -0.382000 | 259 | 168 | 0.350000 | 1830 | -2073 | adult | 63 | 63.100000 | cross | False |
20 | 2013_03_19 | II | 1 | 1612 | 18 | 3.790000 | -655 | 3087 | 1.540000 | -0.385000 | 348 | 15 | 0.960000 | 4633 | -1881 | adult | 70 | 72.700000 | cross | True |
21 | 2013_03_19 | II | 2 | 605 | 55 | 1.420000 | -292 | 1261 | 0.690000 | -0.294000 | 248 | 24 | 0.900000 | 2441 | -1177 | adult | 70 | 72.700000 | cross | False |
22 | 2013_03_19 | II | 3 | 327 | 51 | 0.770000 | -246 | 1508 | 0.580000 | -0.282000 | 130 | 34 | 0.740000 | 2517 | -1894 | adult | 70 | 72.700000 | cross | False |
23 | 2013_03_19 | II | 4 | 946 | 59 | 2.230000 | -245 | 1841 | 0.580000 | -0.340000 | 106 | 26 | 0.760000 | 8893 | -2301 | adult | 70 | 72.700000 | cross | False |
24 | 2013_03_21 | II | 1 | 541 | 33 | 1.270000 | -553 | 3126 | 1.300000 | -0.432000 | 276 | 16 | 0.940000 | 1959 | -2004 | adult | 70 | 72.700000 | cross | False |
25 | 2013_03_21 | II | 2 | 1539 | 43 | 3.620000 | -664 | 741 | 1.560000 | -0.046000 | 85 | 24 | 0.720000 | 18073 | -7802 | adult | 70 | 72.700000 | cross | False |
26 | 2013_03_21 | II | 3 | 529 | 28 | 1.240000 | -261 | 2482 | 0.610000 | -0.414000 | 325 | 33 | 0.900000 | 1627 | -803 | adult | 70 | 72.700000 | cross | False |
27 | 2013_03_21 | II | 4 | 628 | 31 | 1.480000 | -691 | 998 | 1.630000 | -0.071000 | 242 | 67 | 0.720000 | 2600 | -2860 | adult | 70 | 72.700000 | cross | False |
28 | 2013_03_25 | II | 1 | 1453 | 72 | 3.420000 | -92 | 1652 | 0.220000 | -0.008000 | 136 | 0 | 1.000000 | 10645 | -678 | adult | 70 | 72.700000 | cross | False |
29 | 2013_03_25 | II | 2 | 297 | 42 | 0.700000 | -566 | 936 | 1.330000 | -0.084000 | 126 | 4 | 0.970000 | 2367 | -4506 | adult | 70 | 72.700000 | cross | False |
30 | 2013_03_25 | II | 3 | 703 | 33 | 1.650000 | -223 | 2152 | 0.520000 | -0.209000 | 237 | 8 | 0.970000 | 2972 | -942 | adult | 70 | 72.700000 | cross | False |
31 | 2013_03_25 | II | 4 | 269 | 57 | 0.630000 | -512 | 189 | 1.200000 | -0.055000 | 29 | 28 | 0.030000 | 9279 | -17652 | adult | 70 | 72.700000 | cross | False |
32 | 2013_03_28 | II | 1 | 751 | 39 | 1.770000 | -227 | 1195 | 0.530000 | -0.026000 | 206 | 0 | 1.000000 | 3647 | -1101 | adult | 70 | 72.700000 | cross | False |
33 | 2013_03_28 | II | 2 | 245 | 21 | 0.580000 | -573 | 1466 | 1.350000 | -0.215000 | 190 | 46 | 0.760000 | 1288 | -3014 | adult | 70 | 72.700000 | cross | False |
34 | 2013_04_03 | II | 1 | 1182 | 28 | 2.780000 | -522 | 1197 | 1.230000 | -0.118000 | 281 | 0 | 1.000000 | 4213 | -1860 | adult | 70 | 72.700000 | cross | False |
35 | 2013_04_03 | II | 2 | 515 | 29 | 1.210000 | -599 | 1486 | 1.410000 | -0.226000 | 217 | 0 | 1.000000 | 2369 | -2757 | adult | 70 | 72.700000 | cross | False |
36 | 2013_04_08 | II | 1 | 435 | 26 | 1.020000 | -364 | 1017 | 0.860000 | -0.211000 | 189 | 89 | 0.530000 | 2302 | -1927 | adult | 70 | 72.700000 | cross | False |
37 | 2013_04_08 | II | 2 | 383 | 31 | 0.900000 | -469 | 974 | 1.100000 | -0.260000 | 221 | 72 | 0.670000 | 1737 | -2129 | adult | 70 | 72.700000 | cross | False |
38 | 2013_04_08 | II | 3 | 457 | 15 | 1.080000 | -844 | 780 | 1.990000 | -0.328000 | 171 | 106 | 0.380000 | 2665 | -4925 | adult | 70 | 72.700000 | cross | False |
39 | 2013_04_12 | II | 1 | 730 | 42 | 1.720000 | -648 | 786 | 1.520000 | -0.121000 | 142 | 43 | 0.700000 | 5149 | -4573 | adult | 70 | 72.700000 | cross | False |
40 | 2013_05_27 | III | 1 | 614 | 88 | 4.940000 | -94 | 683 | 0.760000 | -0.001000 | 97 | 15 | 0.830000 | 6326 | -967 | juvenile | 28 | 12.700000 | cranwelli | False |
41 | 2013_05_27 | III | 2 | 414 | 143 | 3.330000 | -163 | 245 | 1.310000 | -0.032000 | 108 | 10 | 0.600000 | 3824 | -1507 | juvenile | 28 | 12.700000 | cranwelli | False |
42 | 2013_05_27 | III | 3 | 324 | 105 | 2.610000 | -172 | 619 | 1.380000 | -0.079000 | 55 | 23 | 0.370000 | 5946 | -3149 | juvenile | 28 | 12.700000 | cranwelli | False |
43 | 2013_06_11 | III | 1 | 776 | 35 | 6.240000 | -225 | 1823 | 1.810000 | -0.132000 | 124 | 17 | 0.770000 | 6272 | -1818 | juvenile | 28 | 12.700000 | cranwelli | False |
44 | 2013_06_11 | III | 2 | 611 | 29 | 4.910000 | -301 | 918 | 2.420000 | -0.155000 | 128 | 43 | 0.020000 | 4770 | -2354 | juvenile | 28 | 12.700000 | cranwelli | False |
45 | 2013_06_11 | III | 3 | 544 | 16 | 4.380000 | -93 | 1351 | 0.750000 | -0.110000 | 43 | 34 | 0.710000 | 12699 | -2181 | juvenile | 28 | 12.700000 | cranwelli | False |
46 | 2013_06_14 | III | 1 | 538 | 38 | 4.320000 | -131 | 1790 | 1.050000 | -0.036000 | 130 | 74 | 1.000000 | 4130 | -1005 | juvenile | 28 | 12.700000 | cranwelli | False |
47 | 2013_06_14 | III | 2 | 579 | 31 | 4.660000 | -289 | 1006 | 2.330000 | -0.073000 | 113 | 4 | 0.480000 | 5110 | -2555 | juvenile | 28 | 12.700000 | cranwelli | False |
48 | 2013_06_18 | III | 1 | 806 | 29 | 6.490000 | -104 | 883 | 0.840000 | -0.055000 | 115 | 55 | 0.660000 | 6993 | -902 | juvenile | 28 | 12.700000 | cranwelli | True |
49 | 2013_06_18 | III | 2 | 459 | 32 | 3.700000 | -229 | 1218 | 1.850000 | -0.137000 | 89 | 6 | 0.950000 | 5165 | -2580 | juvenile | 28 | 12.700000 | cranwelli | False |
50 | 2013_06_18 | III | 3 | 458 | 30 | 3.690000 | -259 | 910 | 2.080000 | -0.194000 | 91 | 88 | 0.150000 | 5048 | -2855 | juvenile | 28 | 12.700000 | cranwelli | False |
51 | 2013_06_18 | III | 4 | 626 | 16 | 5.040000 | -231 | 550 | 1.860000 | -0.042000 | 82 | 23 | 0.010000 | 7633 | -2819 | juvenile | 28 | 12.700000 | cranwelli | False |
52 | 2013_06_21 | III | 1 | 621 | 27 | 4.990000 | -267 | 2081 | 2.140000 | -0.183000 | 120 | 58 | 0.900000 | 5152 | -2213 | juvenile | 28 | 12.700000 | cranwelli | False |
53 | 2013_06_21 | III | 2 | 544 | 30 | 4.380000 | -178 | 376 | 1.430000 | -0.034000 | 19 | 17 | 0.050000 | 28641 | -9364 | juvenile | 28 | 12.700000 | cranwelli | False |
54 | 2013_06_21 | III | 3 | 535 | 35 | 4.300000 | -123 | 289 | 0.990000 | -0.029000 | 21 | 29 | 0.050000 | 25471 | -5843 | juvenile | 28 | 12.700000 | cranwelli | False |
55 | 2013_06_21 | III | 4 | 385 | 39 | 3.090000 | -151 | 607 | 1.220000 | -0.082000 | 31 | 126 | 0.030000 | 12409 | -4882 | juvenile | 28 | 12.700000 | cranwelli | False |
56 | 2013_06_26 | III | 1 | 401 | 36 | 3.230000 | -127 | 2932 | 1.020000 | -0.215000 | 142 | 12 | 0.860000 | 2835 | -896 | juvenile | 28 | 12.700000 | cranwelli | False |
57 | 2013_06_26 | III | 2 | 614 | 34 | 4.940000 | -372 | 680 | 2.990000 | -0.140000 | 72 | 1 | 0.420000 | 8475 | -5136 | juvenile | 28 | 12.700000 | cranwelli | False |
58 | 2013_06_26 | III | 3 | 665 | 40 | 5.350000 | -236 | 685 | 1.900000 | -0.118000 | 129 | 0 | 0.160000 | 5171 | -1834 | juvenile | 28 | 12.700000 | cranwelli | False |
59 | 2013_06_26 | III | 4 | 488 | 34 | 3.930000 | -390 | 1308 | 3.140000 | -0.208000 | 112 | 58 | 0.390000 | 4376 | -3492 | juvenile | 28 | 12.700000 | cranwelli | False |
60 | 2013_05_27 | IV | 2 | 172 | 26 | 1.280000 | -456 | 462 | 3.400000 | -0.050000 | 133 | 0 | 0.880000 | 1297 | -3440 | juvenile | 31 | 12.700000 | cranwelli | False |
61 | 2013_05_27 | IV | 3 | 142 | 20 | 1.050000 | -193 | 250 | 1.440000 | -0.047000 | 57 | 74 | 0.830000 | 2498 | -3400 | juvenile | 31 | 12.700000 | cranwelli | False |
62 | 2013_05_27 | IV | 4 | 37 | 55 | 0.280000 | -236 | 743 | 1.760000 | -0.119000 | 51 | 44 | 0.540000 | 735 | -4647 | juvenile | 31 | 12.700000 | cranwelli | False |
63 | 2013_05_30 | IV | 1 | 453 | 38 | 3.370000 | -225 | 844 | 1.680000 | -0.110000 | 142 | 108 | 0.480000 | 3177 | -1581 | juvenile | 31 | 12.700000 | cranwelli | False |
64 | 2013_05_30 | IV | 2 | 355 | 31 | 2.640000 | -217 | 728 | 1.610000 | -0.023000 | 174 | 39 | 0.980000 | 2037 | -1245 | juvenile | 31 | 12.700000 | cranwelli | False |
65 | 2013_05_30 | IV | 3 | 22 | 33 | 0.170000 | -161 | 472 | 1.200000 | -0.052000 | 56 | 4 | 0.020000 | 397 | -2866 | juvenile | 31 | 12.700000 | cranwelli | False |
66 | 2013_06_03 | IV | 1 | 502 | 74 | 3.740000 | -139 | 959 | 1.040000 | -0.089000 | 65 | 77 | 0.910000 | 7713 | -2141 | juvenile | 31 | 12.700000 | cranwelli | False |
67 | 2013_06_11 | IV | 1 | 273 | 26 | 2.030000 | -264 | 844 | 1.970000 | -0.160000 | 124 | 81 | 0.290000 | 2205 | -2136 | juvenile | 31 | 12.700000 | cranwelli | False |
68 | 2013_06_11 | IV | 2 | 720 | 27 | 5.360000 | -342 | 1515 | 2.550000 | -0.226000 | 137 | 0 | 0.830000 | 5259 | -2497 | juvenile | 31 | 12.700000 | cranwelli | False |
69 | 2013_06_11 | IV | 3 | 582 | 33 | 4.340000 | -231 | 279 | 1.720000 | -0.033000 | 60 | 4 | 0.030000 | 9705 | -3847 | juvenile | 31 | 12.700000 | cranwelli | False |
70 | 2013_06_11 | IV | 4 | 198 | 23 | 1.470000 | -209 | 1427 | 1.550000 | -0.151000 | 110 | 69 | 0.840000 | 1793 | -1889 | juvenile | 31 | 12.700000 | cranwelli | False |
71 | 2013_06_14 | IV | 1 | 198 | 6 | 1.470000 | -292 | 2874 | 2.170000 | -0.232000 | 145 | 50 | 0.990000 | 1369 | -2018 | juvenile | 31 | 12.700000 | cranwelli | False |
72 | 2013_06_18 | IV | 1 | 597 | 29 | 4.440000 | -339 | 4251 | 2.530000 | -0.281000 | 191 | 12 | 1.000000 | 3116 | -1772 | juvenile | 31 | 12.700000 | cranwelli | False |
73 | 2013_06_18 | IV | 2 | 516 | 31 | 3.840000 | -371 | 626 | 2.760000 | -0.094000 | 83 | 18 | 0.120000 | 6184 | -4447 | juvenile | 31 | 12.700000 | cranwelli | False |
74 | 2013_06_18 | IV | 3 | 815 | 34 | 6.070000 | -331 | 1254 | 2.470000 | -0.077000 | 151 | 20 | 0.710000 | 5386 | -2190 | juvenile | 31 | 12.700000 | cranwelli | True |
75 | 2013_06_18 | IV | 4 | 402 | 38 | 3.000000 | -302 | 986 | 2.250000 | -0.122000 | 117 | 30 | 0.070000 | 3446 | -2591 | juvenile | 31 | 12.700000 | cranwelli | False |
76 | 2013_06_21 | IV | 1 | 605 | 39 | 4.500000 | -216 | 1627 | 1.610000 | -0.139000 | 123 | 20 | 1.000000 | 4928 | -1759 | juvenile | 31 | 12.700000 | cranwelli | False |
77 | 2013_06_21 | IV | 2 | 711 | 76 | 5.300000 | -163 | 2021 | 1.210000 | -0.217000 | 129 | 42 | 0.970000 | 5498 | -1257 | juvenile | 31 | 12.700000 | cranwelli | False |
78 | 2013_06_21 | IV | 3 | 614 | 33 | 4.570000 | -367 | 1366 | 2.730000 | -0.198000 | 128 | 108 | 0.460000 | 4776 | -2857 | juvenile | 31 | 12.700000 | cranwelli | False |
79 | 2013_06_21 | IV | 4 | 468 | 36 | 3.480000 | -218 | 1269 | 1.630000 | -0.122000 | 129 | 68 | 0.610000 | 3617 | -1688 | juvenile | 31 | 12.700000 | cranwelli | False |
Computing environment
[9]:
%load_ext watermark
%watermark -v -p pandas,jupyterlab
Python implementation: CPython
Python version : 3.11.9
IPython version : 8.20.0
pandas : 2.2.1
jupyterlab: 4.0.13