測試環境為 CentOS 8 (虛擬機)
參考資料 – https://ithelp.ithome.com.tw/articles/10251664
安裝所需模組
[root@localhost ~]# pip install pandas
匯入模組
[root@localhost ~]# python3 Python 3.6.8 (default, Sep 10 2021, 09:13:53) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pandas as pd
從網路抓的資料大多是 JSON 格式,下面就以此為範例.
>>> data = pd.DataFrame({ "Chinese": { "Ben": 68, "Alex": 86, "Jeff": 57 }, "English": { "Ben": 63, "Alex": 92, "Jeff": 83 }, "Math": { "Ben": 65, "Alex": 89, "Jeff": 77 } })
>>> data Chinese English Math Ben 68 63 65 Alex 86 92 89 Jeff 57 83 77
使用索引名稱變更值
DataFrame.loc[“索引名稱”, “欄位名稱”]=值
>>> data.loc["Ben","Chinese"]=99
>>> data Chinese English Math Ben 99 63 65 Alex 86 92 89 Jeff 57 83 77
刪除資料
DataFrame.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors=’raise’)
- labels – 索引名稱或欄位名稱可為單一 Label 或是 List
- axis – 是用來決定刪除的是行還是列, 預設為 0 表示為列(前面需指定索引名稱), 1 表示為欄 (前面需指定欄位名稱)
- index – Column index to drop
- columns – single label or list-like.
- level – int or level name, optional, use for Multiindex.
- inplace – Default False and returns a copy of DataFrame. When used True, it drop’s column inplace and returns None.
- errors – {‘ignore’, ‘raise’}, default ‘raise’
下面來看一下要怎麼刪除 行 或是 列 的資料.
- 刪除 Ben 這一行的資料
>>> data1=data.drop("Ben")
>>> data1 Chinese English Math Alex 86 92 89 Jeff 57 83 77
>>> data Chinese English Math Ben 99 63 65 Alex 86 92 89 Jeff 57 83 77
- 刪除 Chinese 欄位資料
>>> data.drop("Chinese",axis=1) English Math Ben 63 65 Alex 92 89 Jeff 83 77
新增行資料
DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=False)
- other – 須為 DataFrame 或是 Series/dict-like object, 以及 list of these.
- ignore_index – bool, default False. When set to True, It creates axis with incremental numeric number.
- verify_integrity – bool, default False. When set to True, raises error for duplicate index.
- sort – bool, default False.
這邊新增資料使用 DataFrame 格式.
data1 = pd.DataFrame({ "Chinese": { "Ken": 68 }, "English": { "Ken": 63, }, "Math": { "Ken": 65, } })
>>> data.append(data1) Chinese English Math Ben 99 63 65 Alex 86 92 89 Jeff 57 83 77 Ken 68 63 65
新增列資料
有以下幾種方式來新增列的資料.
data["SUM"]=data.sum(axis=1) >>> data Chinese English Math SUM Ben 99 63 65 227 Alex 86 92 89 267 Jeff 57 83 77 217
>>> data["PE Class"] = [80, 80, 80] >>> data Chinese English Math SUM PE Class Ben 99 63 65 227 80 Alex 86 92 89 267 80 Jeff 57 83 77 217 80
>>> data["From"]=pd.Series(data=["Taipei", "Taichung", "Tainan"] , index=["Ben" , "Alex" , "Jeff"]) >>> data Chinese English Math SUM PE Class From Ben 99 63 65 227 80 Taipei Alex 86 92 89 267 80 Taichung Jeff 57 83 77 217 80 Tainan
沒有解決問題,試試搜尋本站其他內容