gpt4 book ai didi

python - 添加新行并保留索引 python

转载 作者:太空宇宙 更新时间:2023-11-03 14:33:13 28 4
gpt4 key购买 nike

如何将结果向下移动一行,使其一直到索引 2017-11-06 ?我想在2017-10-22的第一行添加0

这是我的日期范围

produktname  NEG_00_04  NEG_04_08  NEG_08_12  NEG_12_16  NEG_16_20  NEG_20_24  \
datum_von
2017-10-22 10.46 9.73 1.85 6.18 1.32 0.68
2017-10-23 8.13 7.39 0.61 0.89 0.51 0.54
2017-10-24 6.11 5.68 0.46 0.84 0.36 0.36
2017-10-25 4.95 4.40 0.28 0.52 0.21 0.24
2017-10-26 4.59 4.02 0.24 0.48 0.16 0.20
2017-10-27 18.01 23.32 12.25 37.96 22.66 54.65
2017-10-28 138.91 140.96 81.27 41.60 13.17 40.82
2017-10-29 142.94 152.55 91.93 78.32 31.26 11.87
2017-10-30 16.54 15.15 1.49 2.09 1.89 3.84
2017-10-31 12.95 10.86 1.92 2.07 1.86 3.04
2017-11-01 9.87 9.12 1.23 1.60 1.12 1.60
2017-11-02 6.79 6.22 0.63 0.88 0.64 0.96
2017-11-03 3.56 3.45 1.11 1.53 1.24 1.46
2017-11-04 11.86 9.10 0.93 1.64 1.76 1.69
2017-11-05 NaN NaN NaN NaN NaN NaN

最佳答案

使用shift :

df = df.shift(1, freq='1D')
print (df)
NEG_00_04 NEG_04_08 NEG_08_12 NEG_12_16 NEG_16_20 NEG_20_24
datum_von
2017-10-23 10.46 9.73 1.85 6.18 1.32 0.68
2017-10-24 8.13 7.39 0.61 0.89 0.51 0.54
2017-10-25 6.11 5.68 0.46 0.84 0.36 0.36
2017-10-26 4.95 4.40 0.28 0.52 0.21 0.24
2017-10-27 4.59 4.02 0.24 0.48 0.16 0.20
2017-10-28 18.01 23.32 12.25 37.96 22.66 54.65
2017-10-29 138.91 140.96 81.27 41.60 13.17 40.82
2017-10-30 142.94 152.55 91.93 78.32 31.26 11.87
2017-10-31 16.54 15.15 1.49 2.09 1.89 3.84
2017-11-01 12.95 10.86 1.92 2.07 1.86 3.04
2017-11-02 9.87 9.12 1.23 1.60 1.12 1.60
2017-11-03 6.79 6.22 0.63 0.88 0.64 0.96
2017-11-04 3.56 3.45 1.11 1.53 1.24 1.46
2017-11-05 11.86 9.10 0.93 1.64 1.76 1.69
2017-11-06 NaN NaN NaN NaN NaN NaN

如果需要删除最后一行:

df = df.shift(1, freq='1D').iloc[:-1]
print (df)
NEG_00_04 NEG_04_08 NEG_08_12 NEG_12_16 NEG_16_20 NEG_20_24
datum_von
2017-10-23 10.46 9.73 1.85 6.18 1.32 0.68
2017-10-24 8.13 7.39 0.61 0.89 0.51 0.54
2017-10-25 6.11 5.68 0.46 0.84 0.36 0.36
2017-10-26 4.95 4.40 0.28 0.52 0.21 0.24
2017-10-27 4.59 4.02 0.24 0.48 0.16 0.20
2017-10-28 18.01 23.32 12.25 37.96 22.66 54.65
2017-10-29 138.91 140.96 81.27 41.60 13.17 40.82
2017-10-30 142.94 152.55 91.93 78.32 31.26 11.87
2017-10-31 16.54 15.15 1.49 2.09 1.89 3.84
2017-11-01 12.95 10.86 1.92 2.07 1.86 3.04
2017-11-02 9.87 9.12 1.23 1.60 1.12 1.60
2017-11-03 6.79 6.22 0.63 0.88 0.64 0.96
2017-11-04 3.56 3.45 1.11 1.53 1.24 1.46
2017-11-05 11.86 9.10 0.93 1.64 1.76 1.69

按 Coldspeed 建议进行编辑:

df = df.shift(1, freq='1D').shift().fillna(0)
print (df)
NEG_00_04 NEG_04_08 NEG_08_12 NEG_12_16 NEG_16_20 NEG_20_24
datum_von
2017-10-23 0.00 0.00 0.00 0.00 0.00 0.00
2017-10-24 10.46 9.73 1.85 6.18 1.32 0.68
2017-10-25 8.13 7.39 0.61 0.89 0.51 0.54
2017-10-26 6.11 5.68 0.46 0.84 0.36 0.36
2017-10-27 4.95 4.40 0.28 0.52 0.21 0.24
2017-10-28 4.59 4.02 0.24 0.48 0.16 0.20
2017-10-29 18.01 23.32 12.25 37.96 22.66 54.65
2017-10-30 138.91 140.96 81.27 41.60 13.17 40.82
2017-10-31 142.94 152.55 91.93 78.32 31.26 11.87
2017-11-01 16.54 15.15 1.49 2.09 1.89 3.84
2017-11-02 12.95 10.86 1.92 2.07 1.86 3.04
2017-11-03 9.87 9.12 1.23 1.60 1.12 1.60
2017-11-04 6.79 6.22 0.63 0.88 0.64 0.96
2017-11-05 3.56 3.45 1.11 1.53 1.24 1.46
2017-11-06 11.86 9.10 0.93 1.64 1.76 1.69

关于python - 添加新行并保留索引 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47132838/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com