loc & iloc 用法: === **最基本用法:用column行抓資料** 假設 animal 是一組dataframe 的資料 animal.Salary 可以抓改行的資料 但是建議寫法為 animal["Salary"] 就會抓出Salary 行的資料 ![](https://hackmd.io/_uploads/r1HRPMv0h.png) ``` # 抓一行資料 animal["Salary"] # 抓 多行資料 animal[["Salary", "Birthday"]] ``` **基本用法:用rows列抓資料** loc 和 iloc 基本用法都是由**列**(index) 抓資料,所以我們給 loc["列的名稱"] iloc[列的位置] 如果想抓很多列可以醬紫寫 ``` # 抓一列 dtype: series animal.loc["LeBron James"] # 抓兩列 pandas 存成DataFrme animal.loc[["Paul George", "Kawhi Leonard"]] # 抓需多列 animal.loc["Otto Porter":"Patrick Beverley"] ``` ``` # 用位置抓 # 抓一列 dtype: series animal.iloc[300] # 抓某些列 存成DataFrme animal.iloc[[100, 200, 300, 400]] # 抓需多列 值得注意的是 這種寫法會抓 400, 401, 402, and 403 aniaml.iloc[400:404] ``` **進一步的用法:用列+行抓資料**: 可以進一步給行(column)的資訊,用行+列抓資料,所以我們給 loc["行的名稱"(col), "列的名稱"(index)] iloc[行的位置(col),列的位置(index)] ==值得注意的是 iloc[100:104, :3]: 這種寫法會抓行100,101,102,103 列會抓0,1,2== 冒號之後的不會抓 如果想抓很多行很多列可以醬紫寫 ``` animal.loc[ ["Russell Westbrook", "Anthony Davis"], ["Team", "Salary"] ] ``` 外面的中刮號代表把裏面的資料在包裹起來變成一組dataframe,不論是基本用法或是進階用法當我們特地抓某幾筆資料的時候都要在多一個中刮號 Reference: https://ithelp.ithome.com.tw/articles/10194006?sc=hot https://jupyter.org/try-jupyter/notebooks/?path=Untitled.ipynb Pandas in or not in === https://stackoverflow.com/questions/19960077/how-to-filter-pandas-dataframe-using-in-and-not-in-like-in-sql ![](https://hackmd.io/_uploads/ryNGjaaTn.png) https://www.geeksforgeeks.org/exporting-a-pandas-dataframe-to-an-excel-file/