【问题标题】:Pandas reorder rows based on smallest to largest values in a columnPandas 根据列中的最小值到最大值对行进行重新排序
【发布时间】:2018-06-01 12:22:39
【问题描述】:

对于大型动态数据,我想根据数据框中的第 9 列值(从最小值到最大值)按升序对行进行重新排序。

我的数据框:

1   2   3   4   5   6   7   8   9  #Column values in this row
a   b   c   d   e   f   g   h   0
a1  b1  c1  d1  dd1 ef  ggg hh  0.5
aaa bbb ccc ddd eee fff ggg dcx -0.5
z   b   c   d   e   f   g   h   55

期望的输出:

1   2   3   4   5   6   7   8   9 #Column values in this row
aaa bbb ccc ddd eee fff ggg dcx -0.5
a   b   c   d   e   f   g   h   0
a1  b1  c1  d1  dd1 ef  ggg hh  0.5
z   b   c   d   e   f   g   h   55

如何重新排序我的数据框行,使其从最低数字到最高数字显示为所需的输出。

df1.sort_values(['9'], ascending=[True])

没有效果。

【问题讨论】:

  • 您是否忘记将其分配给该数据框,即df1= df1.sort_values...
  • 这不会重新排列行。您只需要df3 = df3.sort_values(['9'])

标签: python python-3.x pandas numpy dataframe


【解决方案1】:

你需要参数inplace=True:

df1.sort_values('9', inplace=True)
print (df1)
     1    2    3    4    5    6    7    8     9
2  aaa  bbb  ccc  ddd  eee  fff  ggg  dcx  -0.5
0    a    b    c    d    e    f    g    h   0.0
1   a1   b1   c1   d1  dd1   ef  ggg   hh   0.5
3    z    b    c    d    e    f    g    h  55.0

或按评论分配回Dark:

df1 = df1.sort_values('9')
print (df1)
     1    2    3    4    5    6    7    8     9
2  aaa  bbb  ccc  ddd  eee  fff  ggg  dcx  -0.5
0    a    b    c    d    e    f    g    h   0.0
1   a1   b1   c1   d1  dd1   ef  ggg   hh   0.5
3    z    b    c    d    e    f    g    h  55.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多