【问题标题】:Update index after sorting data-frame排序数据框后更新索引
【发布时间】:2016-01-14 22:14:11
【问题描述】:

采用以下数据框:

x = np.tile(np.arange(3),3)
y = np.repeat(np.arange(3),3)
df = pd.DataFrame({"x": x, "y": y})
   x  y
0  0  0
1  1  0
2  2  0
3  0  1
4  1  1
5  2  1
6  0  2
7  1  2
8  2  2

我需要先按x 排序,然后再按y 排序:

df2 = df.sort(["x", "y"])
   x  y
0  0  0
3  0  1
6  0  2
1  1  0
4  1  1
7  1  2
2  2  0
5  2  1
8  2  2

如何更改索引以使其再次升序。 IE。我怎么得到这个:

   x  y
0  0  0
1  0  1
2  0  2
3  1  0
4  1  1
5  1  2
6  2  0
7  2  1
8  2  2

我尝试了以下方法。不幸的是,它根本不会改变索引:

df2.reindex(np.arange(len(df2.index)))

【问题讨论】:

  • 如果你不需要新的df,试试df.sort(["x", "y"], ignore_index=True, inplace=True)

标签: python pandas


【解决方案1】:

您可以重置使用 reset_index 的索引来取回 0、1、2、...、n-1 的默认索引(并使用 drop=True 表示您想要删除现有索引,而不是将其作为附加列添加到数据框中):

In [19]: df2 = df2.reset_index(drop=True)

In [20]: df2
Out[20]:
   x  y
0  0  0
1  0  1
2  0  2
3  1  0
4  1  1
5  1  2
6  2  0
7  2  1
8  2  2

【讨论】:

  • 这非常有帮助。 exp_data=exp_data.reindex(['year'],axis='columns') 保留旧索引。 Drop 删除旧索引。
【解决方案2】:

由于 pandas 1.0.0 df.sort_values 有一个新参数 ignore_index 可以满足您的需要:

In [1]: df2 = df.sort_values(by=['x','y'],ignore_index=True)

In [2]: df2
Out[2]:
   x  y
0  0  0
1  0  1
2  0  2
3  1  0
4  1  1
5  1  2
6  2  0
7  2  1
8  2  2

【讨论】:

  • 我认为这是 1.0.0 版的新功能。
  • 谢谢!帮助了我!
  • 谢谢。这有帮助
【解决方案3】:

df.sort() 已弃用,请使用df.sort_values(...)https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

然后按照 joris 的回答做 df.reset_index(drop=True)

【讨论】:

    【解决方案4】:

    您可以使用set_index 设置新索引:

    df2.set_index(np.arange(len(df2.index)))
    

    输出:

       x  y
    0  0  0
    1  0  1
    2  0  2
    3  1  0
    4  1  1
    5  1  2
    6  2  0
    7  2  1
    8  2  2
    

    【讨论】:

    • 这个没必要,改用reset_index()
    【解决方案5】:

    以下作品!

    1. 如果你想改变现有的dataframe本身,你可以直接使用

       df.sort_values(by=['col1'], inplace=True)
       df.reset_index(drop=True, inplace=True)
      
       df
       >>     col1  col2  col3 col4
           0    A     2     0    a
           1    A     1     1    B
           2    B     9     9    c
           5    C     4     3    F
           4    D     7     2    e
           3  NaN     8     4    D
      
    2. 另外,如果您不想更改现有数据框,但想将排序后的数据框单独存储到另一个变量中,您可以使用:

      df_sorted = df.sort_values(by=['col1']).reset_index(drop=True)
      
      df_sorted
      >>     col1  col2  col3 col4
          0    A     2     0    a
          1    A     1     1    B
          2    B     9     9    c
          3    C     4     3    F
          4    D     7     2    e
          5  NaN     8     4    D
      
      df
      >>       col1  col2  col3 col4
            0    A     2     0    a
            1    A     1     1    B
            2    B     9     9    c
            3  NaN     8     4    D
            4    D     7     2    e
            5    C     4     3    F
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-05
      • 2019-12-20
      • 2019-02-05
      • 2021-08-10
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多