pivot_wider
In [1]:
Copied!
# https://tidyr.tidyverse.org/reference/pivot_wider.html
%run nb_helpers.py
from datar.data import fish_encounters, us_rent_income, warpbreaks
from datar.all import *
nb_header(pivot_wider)
# https://tidyr.tidyverse.org/reference/pivot_wider.html
%run nb_helpers.py
from datar.data import fish_encounters, us_rent_income, warpbreaks
from datar.all import *
nb_header(pivot_wider)
Try this notebook on binder.
★ pivot_wider¶
"widens" data, increasing the number of columns and decreasing the number of rows.
Args:
_data: A data frame to pivot.
id_cols: A set of columns that uniquely identifies each observation.
Defaults to all columns in data except for the columns specified
in names_from and values_from.
names_from: and
values_from: A pair of arguments describing which column
(or columns) to get the name of the output column (names_from),
and which column (or columns) to get the cell values from
(values_from).
names_prefix: String added to the start of every variable name.
names_sep: If names_from or values_from contains multiple variables,
this will be used to join their values together into a single
string to use as a column name.
names_glue: Instead of names_sep and names_prefix, you can supply
a glue specification that uses the names_from columns
(and special _value) to create custom column names.
names_sort: Should the column names be sorted? If FALSE, the default,
column names are ordered by first appearance.
names_repair: todo
values_fill: Optionally, a (scalar) value that specifies what
each value should be filled in with when missing.
values_fn: Optionally, a function applied to the value in each cell
in the output. You will typically use this when the combination
of `id_cols` and value column does not uniquely identify
an observation.
This can be a dict you want to apply different aggregations to
different value columns.
If not specified, will be `numpy.mean`
Returns:
The pivoted dataframe.
In [2]:
Copied!
fish_encounters
fish_encounters
Out[2]:
fish | station | seen | |
---|---|---|---|
<int64> | <object> | <int64> | |
0 | 4842 | Release | 1 |
1 | 4842 | I80_1 | 1 |
2 | 4842 | Lisbon | 1 |
3 | 4842 | Rstr | 1 |
... | ... | ... | ... |
4 | 4842 | Base_TD | 1 |
109 | 4864 | Release | 1 |
110 | 4864 | I80_1 | 1 |
111 | 4865 | Release | 1 |
112 | 4865 | I80_1 | 1 |
113 | 4865 | Lisbon | 1 |
114 rows × 3 columns
In [3]:
Copied!
fish_encounters >> \
pivot_wider(names_from=f.station, values_from=f.seen)
fish_encounters >> \
pivot_wider(names_from=f.station, values_from=f.seen)
Out[3]:
fish | BCE | BCE2 | BCW | BCW2 | Base_TD | I80_1 | Lisbon | MAE | MAW | Release | Rstr | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
<int64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | <float64> | |
0 | 4842 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
1 | 4843 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
2 | 4844 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
3 | 4845 | NaN | NaN | NaN | NaN | 1.0 | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
4 | 4847 | NaN | NaN | NaN | NaN | NaN | 1.0 | 1.0 | NaN | NaN | 1.0 | NaN |
5 | 4848 | NaN | NaN | NaN | NaN | NaN | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
6 | 4849 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | 1.0 | NaN |
7 | 4850 | 1.0 | NaN | 1.0 | NaN | 1.0 | 1.0 | NaN | NaN | NaN | 1.0 | 1.0 |
8 | 4851 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | 1.0 | NaN |
9 | 4854 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | 1.0 | NaN |
10 | 4855 | NaN | NaN | NaN | NaN | 1.0 | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
11 | 4857 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
12 | 4858 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
13 | 4859 | NaN | NaN | NaN | NaN | 1.0 | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
14 | 4861 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
15 | 4862 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | NaN | NaN | 1.0 | 1.0 |
16 | 4863 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | 1.0 | NaN |
17 | 4864 | NaN | NaN | NaN | NaN | NaN | 1.0 | NaN | NaN | NaN | 1.0 | NaN |
18 | 4865 | NaN | NaN | NaN | NaN | NaN | 1.0 | 1.0 | NaN | NaN | 1.0 | NaN |
In [4]:
Copied!
fish_encounters >> \
pivot_wider(names_from=f.station, values_from=f.seen, values_fill=0)
fish_encounters >> \
pivot_wider(names_from=f.station, values_from=f.seen, values_fill=0)
Out[4]:
fish | BCE | BCE2 | BCW | BCW2 | Base_TD | I80_1 | Lisbon | MAE | MAW | Release | Rstr | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
<int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | <int64> | |
0 | 4842 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 4843 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
2 | 4844 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
3 | 4845 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
4 | 4847 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
5 | 4848 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 |
6 | 4849 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
7 | 4850 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 0 | 0 | 1 | 1 |
8 | 4851 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
9 | 4854 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
10 | 4855 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
11 | 4857 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
12 | 4858 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
13 | 4859 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
14 | 4861 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
15 | 4862 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 1 | 1 |
16 | 4863 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
17 | 4864 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
18 | 4865 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
In [5]:
Copied!
us_rent_income
us_rent_income
Out[5]:
GEOID | NAME | variable | estimate | moe | |
---|---|---|---|---|---|
<int64> | <object> | <object> | <float64> | <float64> | |
0 | 1 | Alabama | income | 24476.0 | 136.0 |
1 | 1 | Alabama | rent | 747.0 | 3.0 |
2 | 2 | Alaska | income | 32940.0 | 508.0 |
3 | 2 | Alaska | rent | 1200.0 | 13.0 |
... | ... | ... | ... | ... | ... |
4 | 4 | Arizona | income | 27517.0 | 148.0 |
99 | 55 | Wisconsin | rent | 813.0 | 3.0 |
100 | 56 | Wyoming | income | 30854.0 | 342.0 |
101 | 56 | Wyoming | rent | 828.0 | 11.0 |
102 | 72 | Puerto Rico | income | NaN | NaN |
103 | 72 | Puerto Rico | rent | 464.0 | 6.0 |
104 rows × 5 columns
In [6]:
Copied!
us_rent_income >> \
pivot_wider(names_from=f.variable, values_from=c(f.estimate, f.moe))
us_rent_income >> \
pivot_wider(names_from=f.variable, values_from=c(f.estimate, f.moe))
Out[6]:
GEOID | NAME | estimate_income | estimate_rent | moe_income | moe_rent | |
---|---|---|---|---|---|---|
<int64> | <object> | <float64> | <float64> | <float64> | <float64> | |
0 | 1 | Alabama | 24476.0 | 747.0 | 136.0 | 3.0 |
1 | 2 | Alaska | 32940.0 | 1200.0 | 508.0 | 13.0 |
2 | 4 | Arizona | 27517.0 | 972.0 | 148.0 | 4.0 |
3 | 5 | Arkansas | 23789.0 | 709.0 | 165.0 | 5.0 |
4 | 6 | California | 29454.0 | 1358.0 | 109.0 | 3.0 |
5 | 8 | Colorado | 32401.0 | 1125.0 | 109.0 | 5.0 |
6 | 9 | Connecticut | 35326.0 | 1123.0 | 195.0 | 5.0 |
7 | 10 | Delaware | 31560.0 | 1076.0 | 247.0 | 10.0 |
8 | 11 | District of Columbia | 43198.0 | 1424.0 | 681.0 | 17.0 |
9 | 12 | Florida | 25952.0 | 1077.0 | 70.0 | 3.0 |
10 | 13 | Georgia | 27024.0 | 927.0 | 106.0 | 3.0 |
11 | 15 | Hawaii | 32453.0 | 1507.0 | 218.0 | 18.0 |
12 | 16 | Idaho | 25298.0 | 792.0 | 208.0 | 7.0 |
13 | 17 | Illinois | 30684.0 | 952.0 | 83.0 | 3.0 |
14 | 18 | Indiana | 27247.0 | 782.0 | 117.0 | 3.0 |
15 | 19 | Iowa | 30002.0 | 740.0 | 143.0 | 4.0 |
16 | 20 | Kansas | 29126.0 | 801.0 | 208.0 | 5.0 |
17 | 21 | Kentucky | 24702.0 | 713.0 | 159.0 | 4.0 |
18 | 22 | Louisiana | 25086.0 | 825.0 | 155.0 | 4.0 |
19 | 23 | Maine | 26841.0 | 808.0 | 187.0 | 7.0 |
20 | 24 | Maryland | 37147.0 | 1311.0 | 152.0 | 5.0 |
21 | 25 | Massachusetts | 34498.0 | 1173.0 | 199.0 | 5.0 |
22 | 26 | Michigan | 26987.0 | 824.0 | 82.0 | 3.0 |
23 | 27 | Minnesota | 32734.0 | 906.0 | 189.0 | 4.0 |
24 | 28 | Mississippi | 22766.0 | 740.0 | 194.0 | 5.0 |
25 | 29 | Missouri | 26999.0 | 784.0 | 113.0 | 4.0 |
26 | 30 | Montana | 26249.0 | 751.0 | 206.0 | 9.0 |
27 | 31 | Nebraska | 30020.0 | 773.0 | 146.0 | 4.0 |
28 | 32 | Nevada | 29019.0 | 1017.0 | 213.0 | 6.0 |
29 | 33 | New Hampshire | 33172.0 | 1052.0 | 387.0 | 9.0 |
30 | 34 | New Jersey | 35075.0 | 1249.0 | 148.0 | 4.0 |
31 | 35 | New Mexico | 24457.0 | 809.0 | 214.0 | 6.0 |
32 | 36 | New York | 31057.0 | 1194.0 | 69.0 | 3.0 |
33 | 37 | North Carolina | 26482.0 | 844.0 | 111.0 | 3.0 |
34 | 38 | North Dakota | 32336.0 | 775.0 | 245.0 | 9.0 |
35 | 39 | Ohio | 27435.0 | 764.0 | 94.0 | 2.0 |
36 | 40 | Oklahoma | 26207.0 | 766.0 | 101.0 | 3.0 |
37 | 41 | Oregon | 27389.0 | 988.0 | 146.0 | 4.0 |
38 | 42 | Pennsylvania | 28923.0 | 885.0 | 119.0 | 3.0 |
39 | 44 | Rhode Island | 30210.0 | 957.0 | 259.0 | 6.0 |
40 | 45 | South Carolina | 25454.0 | 836.0 | 123.0 | 4.0 |
41 | 46 | South Dakota | 28821.0 | 696.0 | 276.0 | 7.0 |
42 | 47 | Tennessee | 25453.0 | 808.0 | 102.0 | 4.0 |
43 | 48 | Texas | 28063.0 | 952.0 | 110.0 | 2.0 |
44 | 49 | Utah | 27928.0 | 948.0 | 239.0 | 6.0 |
45 | 50 | Vermont | 29351.0 | 945.0 | 361.0 | 11.0 |
46 | 51 | Virginia | 32545.0 | 1166.0 | 202.0 | 5.0 |
47 | 53 | Washington | 32318.0 | 1120.0 | 113.0 | 4.0 |
48 | 54 | West Virginia | 23707.0 | 681.0 | 203.0 | 6.0 |
49 | 55 | Wisconsin | 29868.0 | 813.0 | 135.0 | 3.0 |
50 | 56 | Wyoming | 30854.0 | 828.0 | 342.0 | 11.0 |
51 | 72 | Puerto Rico | NaN | 464.0 | NaN | 6.0 |
In [7]:
Copied!
us_rent_income >> \
pivot_wider(
names_from=f.variable,
names_sep=".",
values_from=c(f.estimate, f.moe)
)
us_rent_income >> \
pivot_wider(
names_from=f.variable,
names_sep=".",
values_from=c(f.estimate, f.moe)
)
Out[7]:
GEOID | NAME | estimate.income | estimate.rent | moe.income | moe.rent | |
---|---|---|---|---|---|---|
<int64> | <object> | <float64> | <float64> | <float64> | <float64> | |
0 | 1 | Alabama | 24476.0 | 747.0 | 136.0 | 3.0 |
1 | 2 | Alaska | 32940.0 | 1200.0 | 508.0 | 13.0 |
2 | 4 | Arizona | 27517.0 | 972.0 | 148.0 | 4.0 |
3 | 5 | Arkansas | 23789.0 | 709.0 | 165.0 | 5.0 |
4 | 6 | California | 29454.0 | 1358.0 | 109.0 | 3.0 |
5 | 8 | Colorado | 32401.0 | 1125.0 | 109.0 | 5.0 |
6 | 9 | Connecticut | 35326.0 | 1123.0 | 195.0 | 5.0 |
7 | 10 | Delaware | 31560.0 | 1076.0 | 247.0 | 10.0 |
8 | 11 | District of Columbia | 43198.0 | 1424.0 | 681.0 | 17.0 |
9 | 12 | Florida | 25952.0 | 1077.0 | 70.0 | 3.0 |
10 | 13 | Georgia | 27024.0 | 927.0 | 106.0 | 3.0 |
11 | 15 | Hawaii | 32453.0 | 1507.0 | 218.0 | 18.0 |
12 | 16 | Idaho | 25298.0 | 792.0 | 208.0 | 7.0 |
13 | 17 | Illinois | 30684.0 | 952.0 | 83.0 | 3.0 |
14 | 18 | Indiana | 27247.0 | 782.0 | 117.0 | 3.0 |
15 | 19 | Iowa | 30002.0 | 740.0 | 143.0 | 4.0 |
16 | 20 | Kansas | 29126.0 | 801.0 | 208.0 | 5.0 |
17 | 21 | Kentucky | 24702.0 | 713.0 | 159.0 | 4.0 |
18 | 22 | Louisiana | 25086.0 | 825.0 | 155.0 | 4.0 |
19 | 23 | Maine | 26841.0 | 808.0 | 187.0 | 7.0 |
20 | 24 | Maryland | 37147.0 | 1311.0 | 152.0 | 5.0 |
21 | 25 | Massachusetts | 34498.0 | 1173.0 | 199.0 | 5.0 |
22 | 26 | Michigan | 26987.0 | 824.0 | 82.0 | 3.0 |
23 | 27 | Minnesota | 32734.0 | 906.0 | 189.0 | 4.0 |
24 | 28 | Mississippi | 22766.0 | 740.0 | 194.0 | 5.0 |
25 | 29 | Missouri | 26999.0 | 784.0 | 113.0 | 4.0 |
26 | 30 | Montana | 26249.0 | 751.0 | 206.0 | 9.0 |
27 | 31 | Nebraska | 30020.0 | 773.0 | 146.0 | 4.0 |
28 | 32 | Nevada | 29019.0 | 1017.0 | 213.0 | 6.0 |
29 | 33 | New Hampshire | 33172.0 | 1052.0 | 387.0 | 9.0 |
30 | 34 | New Jersey | 35075.0 | 1249.0 | 148.0 | 4.0 |
31 | 35 | New Mexico | 24457.0 | 809.0 | 214.0 | 6.0 |
32 | 36 | New York | 31057.0 | 1194.0 | 69.0 | 3.0 |
33 | 37 | North Carolina | 26482.0 | 844.0 | 111.0 | 3.0 |
34 | 38 | North Dakota | 32336.0 | 775.0 | 245.0 | 9.0 |
35 | 39 | Ohio | 27435.0 | 764.0 | 94.0 | 2.0 |
36 | 40 | Oklahoma | 26207.0 | 766.0 | 101.0 | 3.0 |
37 | 41 | Oregon | 27389.0 | 988.0 | 146.0 | 4.0 |
38 | 42 | Pennsylvania | 28923.0 | 885.0 | 119.0 | 3.0 |
39 | 44 | Rhode Island | 30210.0 | 957.0 | 259.0 | 6.0 |
40 | 45 | South Carolina | 25454.0 | 836.0 | 123.0 | 4.0 |
41 | 46 | South Dakota | 28821.0 | 696.0 | 276.0 | 7.0 |
42 | 47 | Tennessee | 25453.0 | 808.0 | 102.0 | 4.0 |
43 | 48 | Texas | 28063.0 | 952.0 | 110.0 | 2.0 |
44 | 49 | Utah | 27928.0 | 948.0 | 239.0 | 6.0 |
45 | 50 | Vermont | 29351.0 | 945.0 | 361.0 | 11.0 |
46 | 51 | Virginia | 32545.0 | 1166.0 | 202.0 | 5.0 |
47 | 53 | Washington | 32318.0 | 1120.0 | 113.0 | 4.0 |
48 | 54 | West Virginia | 23707.0 | 681.0 | 203.0 | 6.0 |
49 | 55 | Wisconsin | 29868.0 | 813.0 | 135.0 | 3.0 |
50 | 56 | Wyoming | 30854.0 | 828.0 | 342.0 | 11.0 |
51 | 72 | Puerto Rico | NaN | 464.0 | NaN | 6.0 |
In [8]:
Copied!
us_rent_income >> \
pivot_wider(
names_from=f.variable,
names_glue="{variable}_{_value}",
values_from=c(f.estimate, f.moe)
)
us_rent_income >> \
pivot_wider(
names_from=f.variable,
names_glue="{variable}_{_value}",
values_from=c(f.estimate, f.moe)
)
Out[8]:
GEOID | NAME | income_estimate | rent_estimate | income_moe | rent_moe | |
---|---|---|---|---|---|---|
<int64> | <object> | <float64> | <float64> | <float64> | <float64> | |
0 | 1 | Alabama | 24476.0 | 747.0 | 136.0 | 3.0 |
1 | 2 | Alaska | 32940.0 | 1200.0 | 508.0 | 13.0 |
2 | 4 | Arizona | 27517.0 | 972.0 | 148.0 | 4.0 |
3 | 5 | Arkansas | 23789.0 | 709.0 | 165.0 | 5.0 |
4 | 6 | California | 29454.0 | 1358.0 | 109.0 | 3.0 |
5 | 8 | Colorado | 32401.0 | 1125.0 | 109.0 | 5.0 |
6 | 9 | Connecticut | 35326.0 | 1123.0 | 195.0 | 5.0 |
7 | 10 | Delaware | 31560.0 | 1076.0 | 247.0 | 10.0 |
8 | 11 | District of Columbia | 43198.0 | 1424.0 | 681.0 | 17.0 |
9 | 12 | Florida | 25952.0 | 1077.0 | 70.0 | 3.0 |
10 | 13 | Georgia | 27024.0 | 927.0 | 106.0 | 3.0 |
11 | 15 | Hawaii | 32453.0 | 1507.0 | 218.0 | 18.0 |
12 | 16 | Idaho | 25298.0 | 792.0 | 208.0 | 7.0 |
13 | 17 | Illinois | 30684.0 | 952.0 | 83.0 | 3.0 |
14 | 18 | Indiana | 27247.0 | 782.0 | 117.0 | 3.0 |
15 | 19 | Iowa | 30002.0 | 740.0 | 143.0 | 4.0 |
16 | 20 | Kansas | 29126.0 | 801.0 | 208.0 | 5.0 |
17 | 21 | Kentucky | 24702.0 | 713.0 | 159.0 | 4.0 |
18 | 22 | Louisiana | 25086.0 | 825.0 | 155.0 | 4.0 |
19 | 23 | Maine | 26841.0 | 808.0 | 187.0 | 7.0 |
20 | 24 | Maryland | 37147.0 | 1311.0 | 152.0 | 5.0 |
21 | 25 | Massachusetts | 34498.0 | 1173.0 | 199.0 | 5.0 |
22 | 26 | Michigan | 26987.0 | 824.0 | 82.0 | 3.0 |
23 | 27 | Minnesota | 32734.0 | 906.0 | 189.0 | 4.0 |
24 | 28 | Mississippi | 22766.0 | 740.0 | 194.0 | 5.0 |
25 | 29 | Missouri | 26999.0 | 784.0 | 113.0 | 4.0 |
26 | 30 | Montana | 26249.0 | 751.0 | 206.0 | 9.0 |
27 | 31 | Nebraska | 30020.0 | 773.0 | 146.0 | 4.0 |
28 | 32 | Nevada | 29019.0 | 1017.0 | 213.0 | 6.0 |
29 | 33 | New Hampshire | 33172.0 | 1052.0 | 387.0 | 9.0 |
30 | 34 | New Jersey | 35075.0 | 1249.0 | 148.0 | 4.0 |
31 | 35 | New Mexico | 24457.0 | 809.0 | 214.0 | 6.0 |
32 | 36 | New York | 31057.0 | 1194.0 | 69.0 | 3.0 |
33 | 37 | North Carolina | 26482.0 | 844.0 | 111.0 | 3.0 |
34 | 38 | North Dakota | 32336.0 | 775.0 | 245.0 | 9.0 |
35 | 39 | Ohio | 27435.0 | 764.0 | 94.0 | 2.0 |
36 | 40 | Oklahoma | 26207.0 | 766.0 | 101.0 | 3.0 |
37 | 41 | Oregon | 27389.0 | 988.0 | 146.0 | 4.0 |
38 | 42 | Pennsylvania | 28923.0 | 885.0 | 119.0 | 3.0 |
39 | 44 | Rhode Island | 30210.0 | 957.0 | 259.0 | 6.0 |
40 | 45 | South Carolina | 25454.0 | 836.0 | 123.0 | 4.0 |
41 | 46 | South Dakota | 28821.0 | 696.0 | 276.0 | 7.0 |
42 | 47 | Tennessee | 25453.0 | 808.0 | 102.0 | 4.0 |
43 | 48 | Texas | 28063.0 | 952.0 | 110.0 | 2.0 |
44 | 49 | Utah | 27928.0 | 948.0 | 239.0 | 6.0 |
45 | 50 | Vermont | 29351.0 | 945.0 | 361.0 | 11.0 |
46 | 51 | Virginia | 32545.0 | 1166.0 | 202.0 | 5.0 |
47 | 53 | Washington | 32318.0 | 1120.0 | 113.0 | 4.0 |
48 | 54 | West Virginia | 23707.0 | 681.0 | 203.0 | 6.0 |
49 | 55 | Wisconsin | 29868.0 | 813.0 | 135.0 | 3.0 |
50 | 56 | Wyoming | 30854.0 | 828.0 | 342.0 | 11.0 |
51 | 72 | Puerto Rico | NaN | 464.0 | NaN | 6.0 |
In [9]:
Copied!
warpbreaks
warpbreaks
Out[9]:
breaks | wool | tension | |
---|---|---|---|
<int64> | <object> | <object> | |
0 | 26 | A | L |
1 | 30 | A | L |
2 | 54 | A | L |
3 | 25 | A | L |
4 | 70 | A | L |
5 | 52 | A | L |
6 | 51 | A | L |
7 | 26 | A | L |
8 | 67 | A | L |
9 | 18 | A | M |
10 | 21 | A | M |
11 | 29 | A | M |
12 | 17 | A | M |
13 | 12 | A | M |
14 | 18 | A | M |
15 | 35 | A | M |
16 | 30 | A | M |
17 | 36 | A | M |
18 | 36 | A | H |
19 | 21 | A | H |
20 | 24 | A | H |
21 | 18 | A | H |
22 | 10 | A | H |
23 | 43 | A | H |
24 | 28 | A | H |
25 | 15 | A | H |
26 | 26 | A | H |
27 | 27 | B | L |
28 | 14 | B | L |
29 | 29 | B | L |
30 | 19 | B | L |
31 | 29 | B | L |
32 | 31 | B | L |
33 | 41 | B | L |
34 | 20 | B | L |
35 | 44 | B | L |
36 | 42 | B | M |
37 | 26 | B | M |
38 | 19 | B | M |
39 | 16 | B | M |
40 | 39 | B | M |
41 | 28 | B | M |
42 | 21 | B | M |
43 | 39 | B | M |
44 | 29 | B | M |
45 | 20 | B | H |
46 | 21 | B | H |
47 | 24 | B | H |
48 | 17 | B | H |
49 | 13 | B | H |
50 | 15 | B | H |
51 | 15 | B | H |
52 | 16 | B | H |
53 | 28 | B | H |
In [10]:
Copied!
warpbreaks >> \
pivot_wider(
names_from=f.wool,
values_from=f.breaks,
values_fn = mean
)
warpbreaks >> \
pivot_wider(
names_from=f.wool,
values_from=f.breaks,
values_fn = mean
)
Out[10]:
tension | A | B | |
---|---|---|---|
<object> | <float64> | <float64> | |
0 | H | 24.555556 | 18.777778 |
1 | L | 44.555556 | 28.222222 |
2 | M | 24.000000 | 28.777778 |
In [ ]:
Copied!