根据特定列中的值对数据框行进行排序

时间:2020-03-14 16:44:18

标签: python pandas dataframe

我有这个数据框:

d = {'important1': [1.1, 2.2], 'notimportant1': [1.4, 2.5], 'important2': [3.5, 4.2], 'notimportant2': [1.3,2.0]}
important_lst = ['important1', 'important2']
df = pd.DataFrame(data=d)

enter image description here

我想添加另一列,即important_lst中所有列的等级总和。

例如,在当前数据帧中,

  • 第一行的等级为:1、3、4、2,因此等级的总和为5(1 + 4)
  • 第二行的等级:2、3、4、1,所以等级的总和为6(2 + 4)

2 个答案:

答案 0 :(得分:2)

您需要axis=1上的rank来确定各列中的rank,然后在sum之后并在轴= 1上使用df.loc来过滤{{1} } cols:

important_lst

df['new_col'] = df.rank(1).loc[:,important_lst].sum(1)
print(df)

注意:对浮点数列进行汇总时,如果没有特殊要求将它们四舍五入,则最好将它们保留为浮点数(5.0的整数,即5的整数)。

答案 1 :(得分:1)

使用DataFrame.rank通过列表,求和和最后一次转换为整数来过滤列:

df['new_col'] = df.rank(axis=1)[important_lst].sum(axis=1).astype(int)
print (df)
   important1  notimportant1  important2  notimportant2  new_col
0         1.1            1.4         3.5            1.3        5
1         2.2            2.5         4.2            2.0        6
相关问题