A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/50482 below:

SeriesGroupBy.value_counts sorts when sort=False · Issue #50482 · pandas-dev/pandas · GitHub

In the 2nd example, the column gender is reverse-lexicographically sorted (male then female).

df = DataFrame(
    {
        "gender": ["male", "male", "female", "male", "female", "male"],
        "education": ["low", "medium", "high", "low", "high", "low"],
        "country": ["US", "FR", "US", "FR", "FR", "FR"],
    }
)
gb = df.groupby(["country", "gender"], as_index=True, sort=False)
result = gb.value_counts(sort=False)
print(result)
# country  gender  education
# US       male    low          1
# FR       male    medium       1
# US       female  high         1
# FR       male    low          2
#          female  high         1
# dtype: int64

gb2 = df.groupby(["country", "gender"], as_index=True, sort=False)["education"]
result2 = gb2.value_counts(sort=False)
print(result2)
# country  gender  education
# US       male    low          1
# FR       male    low          2
#                  medium       1
# US       female  high         1
# FR       female  high         1
# Name: education, dtype: int64

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4