得分栏与其他栏

2024-04-26 23:45:53 发布

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

我想对df中有多少其他列大于或等于一个参考列进行排序。给定testdf:

testdf = pd.DataFrame({'RefCol': [10, 20, 30, 40], 
                      'Col1': [11, 19, 29, 40], 
                      'Col2': [12, 21, 28, 39], 
                      'Col3': [13, 22, 31, 38] 
                      })

我正在使用helper函数:

def sorter(row):
    sortedrow = row.sort_values()
    return sortedrow.index.get_loc('RefCol')

作为:

testdf['Score'] = testdf.apply(sorter, axis=1)

用实际数据这种方法速度很慢,如何加速呢?谢谢


Tags: 函数helperdataframedf排序defcol2col3