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 显示十进制数的精度
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章