0

我有一个数据框,并按两个键对其进行分组df.groupby(['key1',key2'])。对于每个 key2 条目,如何显示其 key1 值的百分比?

4

2 回答 2

6

Here's an alternative method using one groupby statement.

Group by k1, select column k2 and apply a lambda function. The lambda gets frequency counts for each level of k2 within k1 and then we divide by the count of k1:

In [1]: df.groupby('k1')['k2'].apply(lambda x: pd.value_counts(x)/x.count().astype(float))

Out[1]:
k1
a   x    0.500000
    y    0.500000
b   y    0.666667
    x    0.333333

Performance:

HYRY's method:

100 loops, best of 3: 3.07 ms per loop

My method:

1000 loops, best of 3: 1.98 ms per loop
于 2013-03-29T13:08:37.037 回答
3

为“k1”和(“k1”,“k2”)调用 groupby 两次,然后执行div

import pandas as pd
k1 = ["a", "a", "a", "a", "b", "b", "b"]
k2 = ["x", "x", "y", "y", "x", "y", "y"]
df = pd.DataFrame({"k1":k1, "k2":k2})

df.groupby(["k1", "k2"]).k2.count().div(
    df.groupby("k1").k1.count().astype(float), level=0)

输出:

k1  k2
a   x     0.500000
    y     0.500000
b   x     0.333333
    y     0.666667
于 2013-03-29T11:29:25.697 回答