简体   繁体   English

基于列值的 Pandas 排序

[英]Pandas ordering based on column value

I have a pandas dataframe like:我有一个熊猫数据框,如:

I have the data frame as like below one,我有如下所示的数据框,

Input DataFrame
     id          ratio
 0   1           5.00%
 1   2           9.00%
 2   3           6.00%
 3   2           13.00%
 4   1           19.00%
 5   4           30.00%
 6   3           5.5%
 7   2           22.00%

How can I then group this like我如何才能像这样分组

         id          ratio
     0   1           5.00%
     4   1           19.00%
     6   3           5.5%
     2   3           6.00%
     1   2           9.00%
     3   2           13.00%
     7   2           22.00%
     5   4           30.00%


So essentially first looks at the ratio, takes the lowest for that value and groups the rest of the rows for which it has the same id.所以基本上首先查看比率,取该值的最低值,并将其具有相同 id 的其余行分组。 Then looks for the second lowest ratio and groups the rest of the ids again etc.然后寻找第二低的比率并再次对其余的 id 进行分组等等。

First convert your ratio column to numeric.首先将您的ratio列转换为数字。

Then we get the lowest rank per group by using Groupby然后我们通过使用Groupby获得每组的最低rank

Finally we sort based on rank and numeric ratio .最后,我们根据ranknumeric ratio排序。

df['ratio_num'] = df['ratio'].str[:-1].astype(float).rank()
df['rank'] = df.groupby('id')['ratio_num'].transform('min')

df = df.sort_values(['rank', 'ratio_num']).drop(columns=['rank', 'ratio_num'])

   id   ratio
0   1   5.00%
1   1  19.00%
2   3    5.5%
3   3   6.00%
4   2   9.00%
5   2  13.00%
6   2  22.00%
7   4  30.00%

With help of pd.Categorical :pd.Categorical帮助下:

d = {'id':[1, 2, 3, 2, 1, 4, 3, 2],
     'ratio': ['5.00%', '9.00%', '6.00%', '13.00%', '19.00%', '30.00%', '5.5%', '22.00%']}

df = pd.DataFrame(d)

df['ratio_'] = df['ratio'].map(lambda x: float(x[:-1]))
df['id'] = pd.Categorical(df['id'], categories=df.sort_values(['id', 'ratio_']).groupby('id').head(1).sort_values(['ratio_', 'id'])['id'], ordered=True)
print(df.sort_values(['id', 'ratio_']).drop('ratio_', axis=1))

Prints:印刷:

  id   ratio
0  1   5.00%
4  1  19.00%
6  3    5.5%
2  3   6.00%
1  2   9.00%
3  2  13.00%
7  2  22.00%
5  4  30.00%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM