@@ -28,35 +28,32 @@ def tabyl(
28
28
Returns:
29
29
A DataFrame representing the summary table.
30
30
31
+ Example :
31
32
>>> data = {
32
- ... "Category": ["A", "B", "A", "B", "A", "B"],
33
- ... "Subcategory": ["X", "X", "Y", "Y", "X", "X"],
34
- ... "Value": [1, 2, 3, 4, 5, 6],
33
+ ... "Category": ["A", "A", "B", "B", "C", "C", "A", "B", "C", "A"],
34
+ ... "Subcategory": ["X", "Y", "X", "Y", "X", "Y", "X", "Y", "X", "X"],
35
+ ... "Region": ["North", "South", "East", "West", "North",
36
+ ... "South", "East", "West", "North", "East"],
37
+ ... "Value": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
35
38
... }
36
39
>>> df = pd.DataFrame(data)
37
40
38
- >>> result = adorn_percentages (df, col1= "Category",
39
- col2="Subcategory", axis ="row", fmt=True, include_ns=True )
41
+ >>> result = tabyl (df, "Category", "Subcategory", show_percentages=True ,
42
+ ... percentage_axis ="row")
40
43
>>> print(result)
41
- | Category | X | Y |
42
- |----------|---------|---------|
43
- | A | 50.0% (2) | 50.0% (2) |
44
- | B | 66.7% (2) | 33.3% (1) |
44
+ Subcategory Category X Y
45
+ 0 A 3.0 (75.00%) 1.0 (25.00%)
46
+ 1 B 1.0 (33.33%) 2.0 (66.67%)
47
+ 2 C 2.0 (66.67%) 1.0 (33.33%)
45
48
46
- >>> result = adorn_percentages (df, col1= "Category",
47
- col2="Subcategory", axis ="col", fmt=True )
49
+ >>> result = tabyl (df, "Category", "Subcategory ",
50
+ ... show_percentages=True, percentage_axis ="col")
48
51
>>> print(result)
49
- | Category | X | Y |
50
- |----------|---------|---------|
51
- | A | 33.3% (1) | 50.0% (3) |
52
- | B | 66.7% (2) | 50.0% (3) |
53
-
54
- In this example:
55
- - The table shows counts and row-wise percentages for the `Category` and
56
- `Subcategory` columns.
57
- - The first row for Category 'A' shows 50.0% for X and 50.0% for Y,
58
- with counts in parentheses.
59
- - The second row for Category 'B' shows 66.7% for X and 33.3% for Y.
52
+ Subcategory Category X Y
53
+ 0 A 3.0 (50.00%) 1.0 (25.00%)
54
+ 1 B 1.0 (16.67%) 2.0 (50.00%)
55
+ 2 C 2.0 (33.33%) 1.0 (25.00%)
56
+
60
57
"""
61
58
62
59
if col1 not in df .columns :
@@ -132,28 +129,29 @@ def adorn_totals(df, col1, col2, axis=0):
132
129
:param axis: 0 to add a 'Total' row, 1 to add a 'Total' column
133
130
:return: DataFrame with a 'Total' row/column added
134
131
135
- Example:
136
- >>> data = {
137
- ... "Category": ["A", "B", "A", "B", "A", "B", "A", "B"],
138
- ... "Subcategory": ["X", "X", "Y", "Y", "X", "X", "Y", "Y"],
139
- ... "Value": [1, 2, 3, 4, 5, 6, 7, 8],
140
- ... }
141
- >>> df = pd.DataFrame(data)
142
-
143
- >>> result = adorn_totals(df, col1="Category", col2="Subcategory", axis=0)
144
- >>> print(result)
145
- Category X Y Total
146
- A 6 12 18
147
- B 6 12 18
148
- Total 12 24 36
132
+ Example:
133
+ >>> data = {
134
+ ... "Category": ["A", "B", "A", "B", "A", "B", "A", "B"],
135
+ ... "Subcategory": ["X", "X", "Y", "Y", "X", "X", "Y", "Y"],
136
+ ... "Value": [1, 2, 3, 4, 5, 6, 7, 8],
137
+ ... }
138
+ >>> df = pd.DataFrame(data)
139
+
140
+ >>> result = adorn_totals(df, "Category", "Subcategory", axis=0)
141
+ >>> print(result)
142
+ Subcategory Category X Y
143
+ 0 A 2 2
144
+ 1 B 2 2
145
+ Total NaN 4 4
146
+
147
+ >>> result = adorn_totals(df, "Category", "Subcategory", axis=1)
148
+ >>> print(result)
149
+ Subcategory Category X Y Total
150
+ 0 A 2 2 4
151
+ 1 B 2 2 4
149
152
150
- >>> result = adorn_totals(df, col1="Category", col2="Subcategory", axis=1)
151
- >>> print(result)
152
- Category X Y Total
153
- A 3 3 6
154
- B 3 3 6
155
- Total 6 6 12
156
153
"""
154
+
157
155
# Generate the crosstab using tabyl with the two specified columns
158
156
pivot = tabyl (df , col1 , col2 )
159
157
@@ -182,47 +180,22 @@ def adorn_percentages(
182
180
df , col1 , col2 , axis = "row" , fmt = True , include_ns = False , decimal_places = 1
183
181
):
184
182
"""
185
- Adds percentages to a crosstab generated by tabyl, with options to format
186
- and include raw counts, and also control the behavior
187
- of adorn_pct_formatting and adorn_ns.
188
-
189
- :param df: DataFrame used to generate the crosstab
190
- :param col1: First column to create the crosstab
191
- :param col2: Second column to create the crosstab
192
- :param axis: 'row' to add percentages by row, 'col' for column percentages,
193
- 'all' for global percentages
194
- :param fmt: If True, formats percentages as strings
195
- (e.g., "12.5%"), else returns numeric values.
196
- :param include_ns: If True, includes raw counts alongside percentages.
197
- :param decimal_places: Number of decimal places for the percentages
198
- :param thousand_separator: Whether to add a thousand separator to the counts
199
- :param percent_format: Whether to format as percentages
200
- :return: DataFrame with percentages and optional formatting and raw counts
201
-
202
- Example:
203
- Example:
204
- >>> data = {
205
- ... "Category": ["A", "B", "A", "B", "A", "B"],
206
- ... "Subcategory": ["X", "X", "Y", "Y", "X", "X"],
207
- ... "Value": [1, 2, 3, 4, 5, 6],
208
- ... }
209
- >>> df = pd.DataFrame(data)
210
-
211
- >>> result = adorn_percentages(df, col1="Category",
212
- col2="Subcategory", axis="row", fmt=True, include_ns=True)
213
-
214
- >>> print(result)
215
- Category X Y
216
- A 20% (1) 80% (4)
217
- B 33.33% (2) 66.67% (4)
218
-
219
- >>> result = adorn_percentages(df, col1="Category",
220
- col2="Subcategory", axis="col", fmt=True)
221
-
222
- >>> print(result)
223
- Category X Y
224
- A 33.33% (1) 50% (3)
225
- B 66.67% (2) 50% (3)
183
+ Adds percentages to a crosstab generated by tabyl, with options to format
184
+ and include raw counts, and also control the behavior
185
+ of adorn_pct_formatting and adorn_ns.
186
+
187
+ :param df: DataFrame used to generate the crosstab
188
+ :param col1: First column to create the crosstab
189
+ :param col2: Second column to create the crosstab
190
+ :param axis: 'row' to add percentages by row, 'col' for column percentages,
191
+ 'all' for global percentages
192
+ :param fmt: If True, formats percentages as strings
193
+ (e.g., "12.5%"), else returns numeric values.
194
+ :param include_ns: If True, includes raw counts alongside percentages.
195
+ :param decimal_places: Number of decimal places for the percentages
196
+ :param thousand_separator: Whether to add a thousand separator to the counts
197
+ :param percent_format: Whether to format as percentages
198
+ :return: DataFrame with percentages and optional formatting and raw counts
226
199
227
200
"""
228
201
# Generate the crosstab using tabyl with the two specified columns
0 commit comments