Pandas基礎入門(9)Pandas字符串函數和選項自定義

學習彙總:點這裏

字符串函數

Pandas提供了一組字符串函數,可以方便地對字符串數據進行操作。 最重要的是,這些函數忽略(或排除)丟失/NaN值。幾乎這些方法都使用Python字符串函數(請參閱:這裏 )。 因此,將Series對象轉換爲String對象,然後執行該操作。

編號 函數 描述
1 lower() 將Series/Index中的字符串轉換爲小寫。
2 upper() 將Series/Index中的字符串轉換爲大寫。
3 len() 計算字符串長度。
4 strip() 幫助從兩側的系列/索引中的每個字符串中刪除空格(包括換行符)。
5 split(’ ') 用給定的模式拆分每個字符串。
6 cat(sep=’ ') 使用給定的分隔符連接系列/索引元素。
7 get_dummies() 返回具有單熱編碼值的數據幀(DataFrame)。
8 contains(pattern) 如果元素中包含子字符串,則返回每個元素的布爾值True,否則爲False。
9 replace(a,b) 將值a替換爲值b。
10 repeat(value) 重複每個元素指定的次數。
11 count(pattern) 返回模式中每個元素的出現總數。
12 startswith(pattern) 如果系列/索引中的元素以模式開始,則返回true。
13 endswith(pattern) 如果系列/索引中的元素以模式結束,則返回true。
14 find(pattern) 返回模式第一次出現的位置。
15 findall(pattern) 返回模式的所有出現的列表。
16 swapcase() 變換字母大小寫。
17 islower() 檢查系列/索引中每個字符串中的所有字符是否小寫,返回布爾值
18 isupper() 檢查系列/索引中每個字符串中的所有字符是否大寫,返回布爾值
19 isnumeric() 檢查系列/索引中每個字符串中的所有字符是否爲數字,返回布爾值。

選項自定義

Pandas提供API來自定義其行爲的某些方面,大多使用來顯示。
API由五個相關函數組成。它們分別是 :

  1. get_option()
  2. set_option()
  3. reset_option()
  4. describe_option()
  5. option_context()

1.get_option(param)

get_option(param)需要一個參數,並返回下面輸出中給出的值

display.max_rows

顯示默認值。解釋器讀取此值並顯示此值作爲顯示上限的行。

>>>import pandas as pd
>>>"display.max_rows = ", pd.get_option("display.max_rows")
display.max_rows =  60

display.max_columns

顯示默認值,解釋器讀取此值並顯示此值作爲顯示上限的行。

>>>import pandas as pd
>>>"display.max_columns = ", pd.get_option("display.max_columns")
display.max_columns =  20

注意:這裏的60和20是默認配置參數值。

2.set_option(param,value)

set_option需要兩個參數,並將該值設置爲指定的參數值,如下所示:

display.max_rows

使用set_option(),可以更改要顯示的默認行數。

>>>import pandas as pd
>>>"before set display.max_rows = ", pd.get_option("display.max_rows")
before set display.max_rows =  60

>>>pd.set_option("display.max_rows",80)
>>>"after set display.max_rows = ", pd.get_option("display.max_rows")
after set display.max_rows =  80

display.max_columns

使用set_option(),可以更改要顯示的默認行數。

>>>import pandas as pd
>>>"before set display.max_columns = ", pd.get_option("display.max_columns")
before set display.max_rows =  20

>>>pd.set_option("display.max_columns",32)
>>>"after set display.max_columns = ", pd.get_option("display.max_columns")
after set display.max_rows = 32

3.reset_option(param)

reset_option接受一個參數,並將該值設置爲默認值。

display.max_rows

使用reset_option(),可以將該值更改回顯示的默認行數。

>>>import pandas as pd
>>>pd.set_option("display.max_rows",32)
>>>"after set display.max_rows = ", pd.get_option("display.max_rows")
after set display.max_rows =  32

>>>pd.reset_option("display.max_rows")
>>>"reset display.max_rows = ", pd.get_option("display.max_rows")
reset display.max_rows =  60

4.describe_option(param)

describe_option打印參數的描述。

display.max_rows

使用reset_option(),可以將該值更改回顯示的默認行數。

>>>import pandas as pd
>>>pd.describe_option("display.max_rows")
display.max_rows : int
    If max_rows is exceeded, switch to truncate view. Depending on
    `large_repr`, objects are either centrally truncated or printed as
    a summary view. 'None' value means unlimited.

    In case python/IPython is running in a terminal and `large_repr`
    equals 'truncate' this can be set to 0 and pandas will auto-detect
    the height of the terminal and print a truncated object which fits
    the screen height. The IPython notebook, IPython qtconsole, or
    IDLE do not run in a terminal and hence it is not possible to do
    correct auto-detection.
    [default: 60] [currently: 60]

5.option_context()

option_context上下文管理器用於臨時設置語句中的選項。當退出使用塊時,選項值將自動恢復

display.max_rows

使用option_context(),可以臨時設置該值。

>>>import pandas as pd
>>>with pd.option_context("display.max_rows",10):
   print(pd.get_option("display.max_rows"))
   print(pd.get_option("display.max_rows"))

10
10

請參閱第一和第二個打印語句之間的區別。第一個語句打印由option_context()設置的值,該值在上下文中是臨時的。在使用上下文之後,第二個打印語句打印配置的值。常用參數,請參考下表 :

編號 參數 描述
1 display.max_rows 要顯示的最大行數
2 display.max_columns 要顯示的最大列數
3 display.expand_frame_repr 顯示數據幀以拉伸頁面
4 display.max_colwidth 顯示最大列寬
5 display.precision 顯示十進制數的精度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章