Skip to content

Commit 0f126bb

Browse files
committed
Normalize parameter of value_counts edited.
1 parent fae3e80 commit 0f126bb

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

pandas/core/algorithms.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from typing import (
1212
TYPE_CHECKING,
1313
Literal,
14+
Union,
1415
cast,
1516
)
1617
import warnings
@@ -839,7 +840,7 @@ def value_counts_internal(
839840
values,
840841
sort: bool = True,
841842
ascending: bool = False,
842-
normalize: bool = False,
843+
normalize: Union[bool, str] = False,
843844
bins=None,
844845
dropna: bool = True,
845846
) -> Series:
@@ -912,9 +913,11 @@ def value_counts_internal(
912913
if sort:
913914
result = result.sort_values(ascending=ascending)
914915

915-
if normalize:
916+
if normalize is True:
916917
result = result / counts.sum()
917-
918+
elif normalize == "keep":
919+
result = result.astype(str)+'('+(result/counts.sum())\
920+
.apply(lambda x: round(x, 6)).astype(str)+')'
918921
return result
919922

920923

pandas/core/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ def value_counts(
10861086
self,
10871087
sort=sort,
10881088
ascending=ascending,
1089-
normalize=normalize,
1089+
normalize: Union[bool, str] = False,
10901090
bins=bins,
10911091
dropna=dropna,
10921092
)

0 commit comments

Comments
 (0)