使用.apply时的维度问题(值\u计数,bin=x)

2024-04-25 23:16:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用bin将value\u计数应用到以下数据帧

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)

当我不尝试使用垃圾箱时,代码运行良好。你知道吗


Tags: columns数据dataframesizebinvaluenprandom
2条回答

这是一个解决方法,不是确切的答案:

In [174]: pd.concat([df2[i].value_counts(normalize=True,bins=[0,25,50,75,101]).sort_index() for i in df2.columns],axis=1)
Out[174]: 
                    A      B      C      D
(-0.001, 25.0]  0.253  0.231  0.238  0.270
(25.0, 50.0]    0.263  0.246  0.260  0.248
(50.0, 75.0]    0.264  0.278  0.239  0.241
(75.0, 101.0]   0.220  0.245  0.263  0.241

看起来像虫子。你知道吗

但对我来说,用列表理解和^{}

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)

或将sort_index添加到value_countsapply

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

相关问题 更多 >