Python 使用循环计算多列的秩
我有如下数据: 单位 修订版 EPS 利润 c1 23.5 20.5 40.5 c2 21.5 23.5 25.5 c3 40.5 32.5 20.5Python 使用循环计算多列的秩,python,pandas,Python,Pandas,我有如下数据: 单位 修订版 EPS 利润 c1 23.5 20.5 40.5 c2 21.5 23.5 25.5 c3 40.5 32.5 20.5 使用f-strings分配所有列,而不首先通过索引[1:][code>: for i8 in growth_rank.columns[1:]: growth_rank[f"{i8}_rank"]= growth_rank[i8].rank(ascending=False).astype(int) print (grow
使用
f-string
s分配所有列,而不首先通过索引[1:][code>:
for i8 in growth_rank.columns[1:]:
growth_rank[f"{i8}_rank"]= growth_rank[i8].rank(ascending=False).astype(int)
print (growth_rank)
Company rev eps profit rev_rank eps_rank profit_rank
0 c1 23.5 20.5 40.5 2 3 1
1 c2 21.5 23.5 25.5 3 2 2
2 c3 40.5 32.5 20.5 1 1 3
或处理所有列,但不首先由选定者选择,并使用以下内容显示为原始列:
df1 = growth_rank.iloc[:, 1:].rank(ascending=False).astype(int).add_suffix('_rank')
df = growth_rank.join(df1)
print (df)
Company rev eps profit rev_rank eps_rank profit_rank
0 c1 23.5 20.5 40.5 2 3 1
1 c2 21.5 23.5 25.5 3 2 2
2 c3 40.5 32.5 20.5 1 1 3