将标识符行添加到数据框,然后写入Excel文件

时间:2018-07-09 07:37:34

标签: python pandas dataframe

我想添加一行以标识插入到Excel工作表的数据框。 目前,我的数据框看起来像这样-

final StringRequest stringReq = new StringRequest(Request.Method.POST, API_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (Util.IS_DEBUG_LOGGABLE) {
                    Log.d(TAG, "onResponse: Sent Data");
                }
                Toast.makeText(getContext(), R.string.event_started, Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (Util.IS_DEBUG_LOGGABLE) {
                    Log.d(TAG, "onErrorResponse: Data Not Sent Because server could not verify");
                }
                Toast.makeText(getContext(), getString(R.string.msg_data_not_sent, error), Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> pars = new HashMap<>();
                pars.put("Content-Type", "application/x-www-form-urlencoded");
                return pars;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> pars = new HashMap<>();
                pars.put("round_id", roundId);
                pars.put("trn", tourney);
                pars.put("type", spinnerValue);
                return pars;
            }
        };

我想将此数据帧写入excel工作表(我知道我必须使用pd.ExcelWriter),并在开头添加一个额外的标识符行,以便excel工作表中的最终输出看起来像这样

Name Age Sal
John 23  19000
Amy  25  25000
Rock 22  19500

我不知道如何将此标识符行添加到标题之前的数据框中。请帮忙

1 个答案:

答案 0 :(得分:2)

使用参数startrow=1省略第一行并将文本写入第一个单元格,还检查Working with Cell Notation

writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow = 1, index=False)
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

text = 'Employee Salary Database'
worksheet.write(0, 0, text)

writer.save()