Pandas:从频率表中选择百分比最高的列

Osc*_*sca 2 pandas

您好,我有一个数据框,我想从频率表中选择百分比最高的列。

d = {'c1':['a', 'a', 'b', 'b', 'c', 'c'], 'c2':['Low', 'High', 'Low', 'High', 'High', 'High']}
dd = pd.DataFrame(data=d)
dd.groupby('c1')['c2'].value_counts(normalize=True).mul(100)
Run Code Online (Sandbox Code Playgroud)

它将返回一个频率表

c1  c2  
a   High     50.0
    Low      50.0
b   High     50.0
    Low      50.0
c   High    100.0
Name: c2, dtype: float64
Run Code Online (Sandbox Code Playgroud)

我想打印出c百分比最高的100.0

我可以使用max()打印输出100.0,但不知道如何打印输出c

wwn*_*nde 5

让我们尝试reset_index并删除level=1,然后使用idxmax找到最大索引

dd.groupby('c1')['c2'].value_counts(normalize=True).mul(100).reset_index(level=1, drop=True).idxmax()
Run Code Online (Sandbox Code Playgroud)

  • `s = dd.groupby('c1')['c2'].value_counts(normalize=True).mul(100); s.loc[[s.idxmax()]]` (2认同)