numpy Pandas数据框,更改列名称

w41d8nur  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(163)

我有这样一段代码,它创建了一个表,其中包含以前ML代码中的日期和预测

gv = pd.date_range(str(d1), periods=15)
    lst_output = pd.DataFrame(lst_output)
    pred_log = lst_output.apply(np.square)
    pred = pred_log.apply(np.exp)
    dv = pred[0]
    gv = pd.DataFrame(gv)
    gv['Forecast'] = dv
    gv.to_csv('app/forecast.csv')

    return render_template('analyse.html', get_plot = True, data = data,tables=[gv.to_html(classes='data')], titles=gv.columns.values)

现在,输出看起来像这样

但我想做得更好一点,比如用“日期”重命名第二列中的“0”,并删除左侧的“预测”。此外,删除第一列中的粗体数字会很棒!
你能帮帮我吗?谢谢!
编辑:
我已经设法更改了第二列的名称:

gv = gv.rename(columns={gv.columns[0]: 'Days'})

但仍然不知道如何删除'预测'的左边!
另外,这是在HTML中实际显示表格的代码:

<div id="table">
        {% for table in tables %} {{titles[loop.index]}} {{ table|safe }}
      </div>

编辑2:
所以,有了这个代码

gv['Forecast'] = dv
    gv = gv.rename(columns={gv.columns[0]: 'Days'})
    gv.set_index('Days', inplace=True)
    gv.to_csv('app/forecast.csv')

我有这张table:

使用此代码时:

gv['Forecast'] = dv
    gv = gv.rename(columns={gv.columns[0]: 'Days', 'Forecast': ''})
    gv.set_index('Days', inplace=True)
    gv.to_csv('app/forecast.csv')

我有这张table:

ibps3vxo

ibps3vxo1#

下面的代码应该可以做到这一点:

gv = pd.date_range(str(d1), periods=15)
lst_output = pd.DataFrame(lst_output)
pred_log = lst_output.apply(np.square)
pred = pred_log.apply(np.exp)
dv = pred[0]
gv = pd.DataFrame(gv)
gv['Forecast'] = dv

# new code is here:
# this, as you'd found, renames columns
gv.rename(columns={gv.columns[0]: 'Days', 'Forecast': ''})

# the bold column with 0-14 is the index, by setting 'Days' to index,
# you can drop that column - inplace=True just means you don't have 
# to store the returned value, and instead it modifies your "gv" dataframe
# directly
gv.set_index('Days', inplace=True)

gv.to_csv('app/forecast.csv')

return render_template('analyse.html', get_plot = True, data = data,tables=[gv.to_html(classes='data')], titles=gv.columns.values)

相关问题