我正在solver
使用一组实例比较一组八个算法(列),每个实例对每个算法执行一次,参数级别D
从1到10执行.因此,结果数据框将如下所示:
instance D z solver 0 1000_ep0.0075 1 994 threatened 1 1000_ep0.0075 1 993 desc 2 1000_ep0.0075 1 994 degree 3 1000_ep0.0075 1 993 threatened_desc 4 1000_ep0.0075 1 993 threatened_degree 5 1000_ep0.0075 1 994 desc_later 6 1000_ep0.0075 1 994 degree_later 7 1000_ep0.0075 1 993 dyn_degree 8 1000_ep0.0075 2 986 threatened 9 1000_ep0.0075 2 987 desc 10 1000_ep0.0075 2 988 degree 11 1000_ep0.0075 2 987 threatened_desc 12 1000_ep0.0075 2 986 threatened_degree 13 1000_ep0.0075 2 987 desc_later 14 1000_ep0.0075 2 988 degree_later 15 1000_ep0.0075 2 987 dyn_degree ....
凡z
列对应于由算法找到的值(越小越好).
我想在数据帧中添加一列,根据z
每个组合的值对应每个算法的等级
.对于上面的例子,将是这样的:
instance D z solver z_rank 0 1000_ep0.0075 1 994 threatened 2 1 1000_ep0.0075 1 993 desc 1 2 1000_ep0.0075 1 994 degree 2 3 1000_ep0.0075 1 993 threatened_desc 1 4 1000_ep0.0075 1 993 threatened_degree 1 5 1000_ep0.0075 1 994 desc_later 2 6 1000_ep0.0075 1 994 degree_later 2 7 1000_ep0.0075 1 993 dyn_degree 1 8 1000_ep0.0075 2 986 threatened 1 9 1000_ep0.0075 2 987 desc 2 10 1000_ep0.0075 2 988 degree 3 11 1000_ep0.0075 2 987 threatened_desc 2 12 1000_ep0.0075 2 986 threatened_degree 1 13 1000_ep0.0075 2 987 desc_later 2 14 1000_ep0.0075 2 988 degree_later 3 15 1000_ep0.0075 2 987 dyn_degree 2 ...
使用python-pandas
,这是我到目前为止所能得到的:
df.loc[:, 'z_rank'] = df_rg.groupby(['instance', 'D'])['z'].rank() df.head(16) instance D z solver z_rank 0 1000_ep0.0075 1 994 threatened 47.5 1 1000_ep0.0075 1 993 desc 16.5 2 1000_ep0.0075 1 994 degree 47.5 3 1000_ep0.0075 1 993 threatened_desc 16.5 4 1000_ep0.0075 1 993 threatened_degree 16.5 5 1000_ep0.0075 1 994 desc_later 47.5 6 1000_ep0.0075 1 994 degree_later 47.5 7 1000_ep0.0075 1 993 dyn_degree 16.5 8 1000_ep0.0075 2 986 threatened 7.0 9 1000_ep0.0075 2 987 desc 18.5 10 1000_ep0.0075 2 988 degree 44.5 11 1000_ep0.0075 2 987 threatened_desc 18.5 12 1000_ep0.0075 2 986 threatened_degree 7.0 13 1000_ep0.0075 2 987 desc_later 18.5 14 1000_ep0.0075 2 988 degree_later 44.5 15 1000_ep0.0075 2 987 dyn_degree 18.5
这显然不是我想要的.
有人可以帮我吗?
您需要method=dense
在SeriesGroupBy.rank()
组之间增加1的位置:
df['z_rank'] = df.groupby(['instance', 'D'])['z'].rank(method='dense').astype(int)