Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用循环计算多列的秩_Python_Pandas - Fatal编程技术网

Python 使用循环计算多列的秩

Python 使用循环计算多列的秩,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

我有如下数据:

单位 修订版 EPS 利润 c1 23.5 20.5 40.5 c2 21.5 23.5 25.5 c3 40.5 32.5 20.5
使用
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