我想对df中其他多少个cols大于或等于一个引用值进行排序。经测试后:
testdf = pd.DataFrame({'RefCol': [10, 20, 30, 40],
'Col1': [11, 19, 29, 40],
'Col2': [12, 21, 28, 39],
'Col3': [13, 22, 31, 38]
})我使用的是助手函数:
def sorter(row):
sortedrow = row.sort_values()
return sortedrow.index.get_loc('RefCol')作为:
testdf['Score'] = testdf.apply(sorter, axis=1)与实际数据的这种方法是非常缓慢的,如何加快它?谢谢
发布于 2019-07-24 14:24:12
看起来您需要比较RefCol并检查是否有任何列小于RefCol,请使用:
testdf.lt(testdf['RefCol'],axis=0).sum(1)0 0
1 1
2 2
3 2大于等于使用:
testdf.drop('RefCol',1).ge(testdf.RefCol,axis=0).sum(1)https://stackoverflow.com/questions/57185160
复制相似问题