我有一個沒有任何列標題的 excel 檔案。如下圖所示:

我有一個單獨的 JSON 檔案,它向我解釋了 A 列是客戶數量,B 列是他們購買的產品數量……等等。
{
"excel_template": "template.xlsx",
"excel_sheets": [
{"sheet_name": "Daily",
"id_column": "A",
"frequency": "daily"
"header" : "Number of Customers"
}
]
}
我使用pandas.read_excel(headers=None) 函式將這個excel 檔案作為pandas 資料框讀取。
現在在資料框中,我想建立列參考,例如 A 列是客戶數并列印 A 列。
如果不通過excel檔案中列的字母參考手動計算列的索引,我該怎么做。謝謝
uj5u.com熱心網友回復:
您需要知道您擁有的 JSON 檔案是如何記錄的,以便為您提供準確的答案。
但是,默認情況下,pandas 中的列可以像df.columns
要更改一樣輸出
df.columns = ['name1', 'name2',.....]
你可以這樣做。
df.rename(columns = {'A':'name1',F':'name5'},inplace=True)
我不知道列是如何記錄在 JSON 中的,但是...
后 import json
讀取 json 并將此列名稱與
df.columns = json_column_name
或
df2 = pd.read_json('jsonfile')
所以如果它們在列中,那么
df.columns = df2.columns也是可能的。
如果JSON中的列順序與excel檔案不同,則需要創建一個dict并重命名。
uj5u.com熱心網友回復:
您可以使用names引數:
類似名稱陣列,默認無
List of column names to use.
一個簡單的例子:
df = pd.read_excel('tmp.xls', header=None, names=['ID', 'name'])
您可以從 json 檔案中提取名稱作為字串串列,然后填寫names引數。
有關詳細資訊,請參閱pandas 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357739.html
