Skip to main content
 首页 » 编程设计

python之使用 .apply(value_counts, bin=x) 时的维度问题

2025年01月19日32exmyth

我正在尝试使用 bin 将 value_counts 应用于以下数据框

df2 = pd.DataFrame(np.random.randint(0,100,size=(1000, 4)), columns=list('ABCD')) 
df2.apply(pd.value_counts, normalize=True, bins=[0,25,50,75,101]).sort_values(by=['A'], ascending=False) 

但是,当我这样做时,出现以下错误:

ValueError:无法将输入数组从形状 (5) 广播到形状 (4)

当我不尝试使用 bin 时,代码工作正常。

请您参考如下方法:

看起来像 bug 。

但对我来说工作sort_index列表理解和 concat :

L = [pd.value_counts(df2[x], normalize=True, bins=[0,25,50,75,101]).sort_index() for x in df2] 
b = pd.concat(L, 1).sort_values(by=['A'], ascending=False) 

或者在apply中的value_counts中加入sort_index:

b=df2.apply(lambda x: pd.value_counts(x, normalize=True, bins=[0,25,50,75,101]).sort_index()) 

print (b) 
                    A      B      C      D 
(-0.001, 25.0]  0.263  0.273  0.278  0.259 
(25.0, 50.0]    0.251  0.254  0.234  0.255 
(50.0, 75.0]    0.250  0.257  0.240  0.249 
(75.0, 101.0]   0.236  0.216  0.248  0.237