Skip to content

How to compute percentage per variable ? #946

Answered by has2k1
gigaturbo asked this question in Q&A
Discussion options

You must be logged in to vote

When grouping gets complicated, like here where there are nested groups, it is better (and it may be the only way) to precompute the values and have them in a dataframe as well so that a straight mapping to that column is enough.

However in this case, you can get away with this solution that relies on an internal variable PANEL.

import pandas as pd
import plotnine as p9
from mizani.labels import label_percent

d = pd.DataFrame({
    "cat1": ["A","A","B","A","A","B","B","A","B","B","B","A","B","B","B","A","B"],
    "cat2": ["W","X","Y","Z","W","X","Y","Z","W","X","Y","Z","W","X","Y","Z","X"]
}).assign(x=1)

def sum_by(col: pd.Series, by: pd.Series):
    df = pd.DataFrame({col.name: col, "by"

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@gigaturbo
Comment options

Comment options

You must be logged in to vote
2 replies
@gigaturbo
Comment options

@has2k1
Comment options

Answer selected by gigaturbo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants