Pandas数据帧索引列标题下。为什么?

2024-04-19 14:57:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望索引列标题“date”与所有其他列位于同一行。现在,就低一排。我该怎么解决这个问题?在

enter image description here

@app.route('/closeprice', methods=['POST']) #route tells Flask what URL should trigger the function
def closeprice():    
    tickersym = request.form['ticker']
    pandas_ds = get_historical_data(tickersym, start, end, output_format='pandas')
    pandas_ds['closechg']=pandas_ds.close.pct_change() #closing daily change 
    server_data = (pandas_ds.style.applymap(color_negative_red, subset=['closechg']).format({'closechg':"{:.2%}"})).render()
    return render_template('view.html',tickersym=tickersym,tables=[server_data],titles = tickersym)    

Tags: appformat标题pandasdatadateserverds
2条回答

尝试以下操作:

df.columns.name = df.index.name
df.index.name = None
import pandas as pd
import numpy as np

dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=["open","high","low","close"])

new_dates=df.index.set_names("date")
df.index=new_dates

writer = pd.ExcelWriter('Save_Excel.xlsx')
df.to_excel(writer,'page_1',float_format='%.5f')
writer.save()

我试图复制date的值并在表中断言一个名为“date”的新列,然后重置索引,得到了相同的结果。在

enter image description here

您可以通过以“xlsx”形式保存表来获得所需的内容。在

相关问题 更多 >