【问题标题】:Reversing the order of only the rows in Pandas Python [duplicate]仅反转 Pandas Python 中行的顺序 [重复]
【发布时间】:2021-03-25 13:45:44
【问题描述】:

你好有没有办法可以颠倒行的顺序。我使用了函数data1 = data.iloc[::-1],它反转了数据表中的所有值,包括行号,但是我只希望它反转值并保持行不变。所以我想要 row 0 : Open:242.5 row 1 : Open:243.95 等等。在下图中,您还可以看到行是如何从row#; 1862 开始的,应该是row#; 1862

【问题讨论】:

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


【解决方案1】:

您可以在反转索引后使用 reset_index 来重置索引。

data=data.iloc[::-1].reset_index(drop=True)

【讨论】:

    【解决方案2】:

    就在reset_index()之后。

    df.reset_index(drop=True, inplace=True)
    

    【讨论】:

      【解决方案3】:

      如果是有序索引,只需重置索引:

      data1 = data.iloc[::-1].reset_index(drop=True)
      

      或者保存之前的索引并再次应用它,即使索引没有排序也可以:

      indexes = data.index
      data1 = data.iloc[::-1]
      data1.index = indexes
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-13
        • 2016-06-29
        • 2012-11-11
        • 2016-05-16
        • 2012-09-08
        • 1970-01-01
        • 1970-01-01
        • 2021-02-22
        相关资源
        最近更新 更多