【问题标题】:How to arrange df in ascending order and reset the index numbering如何按升序排列 df 并重置索引编号
【发布时间】:2022-12-31 19:56:04
【问题描述】:

我的是关于股票数据。

开盘价 高价 低价 收盘价 WAP 股数 交易数 总成交量 (Rs.) 可交割数量 % Deli。 Qty to Traded Qty Spread High-Low Spread Close-Open Pert Rank 年份 日期
2022-12-30 419.75 421.55 415.55 418.95 417.841704 1573 183 657265.0 954 60.65 6.00 -0.80 0.131558 2022 2022-12-29 412.15 418.40 411.85 415.90 413.236152 1029 117 425220.0 766 74.44 6.55 3.75 0.086360 2022 2022-12-28 411.90 422.05 411.30 415.35 417.917534 2401 217 1003420.0 949 39.53 10.75 3.45 0.128329 2022 2022-12-27 409.60 414.70 407.60 412.70 411.436312 1052 136 432831.0 687 65.30 7.10 3.10 0.066182 2022 2022-12-26 392.00 409.55 389.60 406.35 400.942300 2461 244 986719.0 1550 62.98 19.95 14.35 0.240920 2022 ………………………………………… 2018-01-05 338.75 358.70 338.75 355.65 351.255581 31802 896 11170630.0 15781 49.62 19.95 16.90 0.949153

日期列按降序排列,必须转换为升序。 同时索引即必须转换为升序。即 1、2、3、4。它不应该按降序排列。

我尝试使用 sort_values。它返回非类型对象。

我期待一个数据框。我也试过groupby。有没有别的办法。

【问题讨论】:

  • 你能给我们一个返回 nonetype 的代码示例吗?

标签: pandas group-by


【解决方案1】:

使用 sort_values 对日期值进行排序对我有用

df = pd.DataFrame({'Dates': ['2022-12-30', '2022-12-29','2022-12-28'],'Prices':[100,101,99]})
df
Out[142]: 
        Dates  Prices
0  2022-12-30     100
1  2022-12-29     101
2  2022-12-28      99
df.sort_values('Dates',ascending=True,inplace=True)
df
Out[144]: 
        Dates  Prices
2  2022-12-28      99
1  2022-12-29     101
0  2022-12-30     100

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2023-03-30
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多