我有这个数据:
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)
我想添加另一列,即important_lst
中所有列的级别之和。
例如,在当前数据帧中,
。
发布于 2020-03-14 16:48:11
将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
发布于 2020-03-14 16:47:19
https://stackoverflow.com/questions/60685041
复制相似问题