2024-10-04 05:26:17 发布
网友
我是Python新手,需要一个如何以更快的方式迭代数据帧项的建议? 我的实现:
weights = histCaps.copy() for index, row in histCaps.iterrows(): for column, item in row.iteritems(): weights[column].loc[index] = item/row.sum()
不要循环,最好使用矢量化的^{}和{a2}以获得更好的性能:
histCaps = pd.DataFrame({ 'B':[4,5,4,5,5,4], 'C':[7,8,9,4,2,3], 'D':[1,3,5,7,1,0], 'E':[5,3,6,9,2,4], }) weights = histCaps.div(histCaps.sum(axis=1),axis=0) print (weights) B C D E 0 0.235294 0.411765 0.058824 0.294118 1 0.263158 0.421053 0.157895 0.157895 2 0.166667 0.375000 0.208333 0.250000 3 0.200000 0.160000 0.280000 0.360000 4 0.500000 0.200000 0.100000 0.200000 5 0.363636 0.272727 0.000000 0.363636
细节:
不要循环,最好使用矢量化的^{} 和{a2}以获得更好的性能:
细节:
^{pr2}$相关问题 更多 >
编程相关推荐