[英]pandas,read_excel, usecols with list input generating an empty dataframe
實際上我只想將excel中的特定列讀取到python數據幀中,我的代碼是
import pandas as pd
file = pd.read_excel("3_Plants sorted on PLF age cost.xlsx",sheet_name="Age>25",index_col="Developer",usecols="Name of Project")
但我得到一個空數據幀作為輸出,但是當我使用
import pandas as pd
file = pd.read_excel("3_Plants sorted on PLF age cost.xlsx",sheet_name="Age>25",index_col="Developer",usecols=2)
我得到了想要的結果,
由於我必須使用循環對許多文件執行此操作,並且列的位置不斷變化,因此我必須按其名稱而不是位置。
此外,我無法在數據框中加載完整文件並使用df["column_name"]
作為我的 excel 文件的大小太大(150 MB),這將使我的過程非常緩慢,有時會出現內存錯誤。
提前致謝。
正如 Tomas Farias 所提到的,usecols 不接受單元格值。 一種可能的方法是讀取幾行並找到列的位置,然后第二次讀取文件。
import pandas as pd
col = pd.read_excel("3_Plants sorted on PLF age cost.xlsx",sheet_name="Age>25", nrows=2).columns
k=col.get_loc('Name of Project')+1
file = pd.read_excel("3_Plants sorted on PLF age cost.xlsx", sheet_name="Age>25", index_col="Developer", usecols=k)
您可以將 .xlsx 文件保存/轉換為 .csv,然后使用: pd.read_csv('filename.csv', usecols=[])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.