簡體   English   中英

如何從現有的 dataframe 將指定的列和行寫入 excel 文件?

[英]How to write specified columns and rows to excel file from an existing dataframe?

由於沒有答案,因此重新發布。

如何從現有數據幀將多列寫入 excel 文件中的某一行? 下圖是我的數據框的內容,比如 df2:

在此處輸入圖像描述

我想要做的是當我將數據幀寫入 excel 時,我只想要兩列的前五個值 我可以使用以下代碼為 value.1 列獲取它:

result2[header3] = (df2["Value.1"]).to_numpy()[0:6]

我無法對 Parameter.1 列做同樣的事情: result2 = (df2["Parameter.1"]).to_numpy()[0:6]

Then the error given is: IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices The error is for the "Value .1" 線。

先感謝您

PS:使用pandas

如果我正確理解您需要什么,基本上只將前 5 行數據保存到 excel,您可以使用head()to_excel

import pandas as pd
df.head(5).to_excel(r'C:\Users\....\filename.xlsx')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM