pandas 讀取excel的指定列


不管對於read_csv還是read_excel,現在都有:

usecols : int or list, default None

  • If None then parse all columns,
  • If int then indicates last column to be parsed
  • If list of ints then indicates list of column numbers to be parsed
  • If string then indicates comma separated list of Excel column letters and column ranges (e.g. “A:E” or “A,C,E:F”). Ranges are inclusive of both sides.

而之前版本的:

parse_cols : int or list, default None

Deprecated since version 0.21.0: Pass in usecols instead.

可以看到在0.21.0版本后就已經完全被上面的那個給替代了。

這也算解決了我一個頭疼的問題,總是把兩個read的內容記混了。

所以對於最新版的pandas,只需要:

df = pd.read_excel("data.xlsx", usecols=[0, 5]) # 指定讀取第1列和第6列
# 當然也可以用"A,F"代替[0,5]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM