按重要性选择列名称

时间:2016-11-19 11:36:36

标签: python sorting dataframe selection

我有一个包含6列和浮点值的数据框。所以我需要为每行选择最前面3个colnames(用空格分隔),每行最大值。例如

rowIndex col1 col2 col3 col4 col5 col6 
       1  0.2  0.4  0.1  1.2  2.2  0.2
       2  4.1  2.3  0.1  2.2  0.2  0.2

结果显示如下:

rowIndex importances
       1  col5 col4 col2
       2  col1 col2 col3

1 个答案:

答案 0 :(得分:0)

kick(whatToKick) {
  whatToKick.changeCoord();
}

会产生如下结果:

import pandas as pd
import numpy as np

nrows = 2
ncols = 6
df = pd.DataFrame(np.random.rand(nrows,ncols), index=list(range(nrows)), columns=['col1', 'col2', 'col3', 'col4', 'col5', 'col6'])

print(df)

for row in range(nrows):
    maxCol1, maxCol2, maxCol3 = 0, 0, 0
    for col in range(ncols):
        if(df.iloc[row, col] > df.iloc[row, maxCol1]):
            maxCol3 = maxCol2
            maxCol2 = maxCol1
            maxCol1 = col
        elif(df.iloc[row, col] > df.iloc[row, maxCol2]):
            maxCol3 = maxCol2
            maxCol2 = col
        elif(df.iloc[row, col] > df.iloc[row, maxCol3]):
            maxCol3 = col
    print("{} {} {} {}".format(row+1, df.columns[maxCol1], df.columns[maxCol2], df.columns[maxCol3]))