測試環境為 CentOS 8 (虛擬機) 參考資料 – https://pandas.pydata.org/pandas-docs/version/0.19/generated/pandas.DataFrame.filter.html
透過 filter 函數搭配 regex (標準表示式) 可以依據 column , Row 名稱來搜尋.
安裝所需模組
[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
建立測試用資料.
>>> df = pd.DataFrame({"one":[1, 4],"two":[2,5],"three":[3,6]}, index=["mouse", "rabbit"]) >>> df one two three mouse 1 2 3 rabbit 4 5 6
依據欄位名稱來搜尋.
>>> df.filter(items=['one', 'three']) one three mouse 1 3 rabbit 4 6
配合 regex 來搜尋欄位.
>>> df.filter(regex='e$', axis=1) one three mouse 1 3 rabbit 4 6
>>> df.filter(regex='one|two', axis=1) one two mouse 1 2 rabbit 4 5
>>> df.filter(regex='.*o.*', axis=1) one two mouse 1 2 rabbit 4 5
使用 like + axis=0 可以搜尋 rows 列名稱.
>>> df.filter(like='bbi', axis=0) one two three rabbit 4 5 6
沒有解決問題,試試搜尋本站其他內容