Python中的Groupby和计数范围和分布
我有Python中的Groupby和计数范围和分布,python,pandas,Python,Pandas,我有dx: City Val1 Val2 x1 N1 0.0000 1.0 x2 N1 790.9980 6.0 x3 N2 110.3755 4.0 x4 N1 191.8150 2.0 x5 N2 574.3100 3.0 x6 N1 725.4000 4.0 x7 N3 362.7000 2.0 x8 N1 196.480
dx
:
City Val1 Val2
x1 N1 0.0000 1.0
x2 N1 790.9980 6.0
x3 N2 110.3755 4.0
x4 N1 191.8150 2.0
x5 N2 574.3100 3.0
x6 N1 725.4000 4.0
x7 N3 362.7000 2.0
x8 N1 196.4800 1.0
x9 N3 230.8200 2.0
x10 N1 259.8830 1.0
可复制示例:
dx = pd.DataFrame({'City': {'x1': 'N1',
'x2': 'N1',
'x3': 'N2',
'x4': 'N1',
'x5': 'N2',
'x6': 'N1',
'x7': 'N3',
'x8': 'N1',
'x9': 'N3',
'x10': 'N1'},
'Val1': {'x1': 0.0,
'x2': 790.998,
'x3': 110.3755,
'x4': 191.81500000000003,
'x5': 574.31,
'x6': 725.4,
'x7': 362.7,
'x8': 196.48,
'x9': 230.82,
'x10': 259.883},
'Val2': {'x1': 1.0,
'x2': 6.0,
'x3': 4.0,
'x4': 2.0,
'x5': 3.0,
'x6': 4.0,
'x7': 2.0,
'x8': 1.0,
'x9': 2.0,
'x10': 1.0}})
dx
我想按城市
对它们进行分组,并计算[020040006008001000]
范围内的Val1
预期产出:
(0, 200] (200, 400] (400, 600] (600, 800] (800, 1000]
City
N1 2 1 0 2 0
N2 1 0 1 0 0
N3 0 2 0 0 0
与一起使用,并通过以下方式添加缺少的类别:
如果添加了“使用”或“与”所有类别:
df = dx.pivot_table(index='State', columns=pd.cut(dx['Val1'], bins=bins), aggfunc='size')
与一起使用,并通过以下方式添加缺少的类别: 如果添加了“使用”或“与”所有类别:
df = dx.pivot_table(index='State', columns=pd.cut(dx['Val1'], bins=bins), aggfunc='size')
或者您可以尝试:
df = df.assign(bins=pd.cut(df.Val1, bins=[0, 200, 400, 600, 800, 1000])).pivot_table(
index='City', columns='bins', values='Val2', aggfunc='count')
输出
:
bins (0, 200] (200, 400] (400, 600] (600, 800] (800, 1000]
City
N1 2 1 0 2 0
N2 1 0 1 0 0
N3 0 2 0 0 0
或者您可以尝试:
df = df.assign(bins=pd.cut(df.Val1, bins=[0, 200, 400, 600, 800, 1000])).pivot_table(
index='City', columns='bins', values='Val2', aggfunc='count')
输出
:
bins (0, 200] (200, 400] (400, 600] (600, 800] (800, 1000]
City
N1 2 1 0 2 0
N2 1 0 1 0 0
N3 0 2 0 0 0