pandas describe 函数的参数理解及应用

percentile:它是一个可选参数, 它是一个列表, 如数字的数据类型, 应在0到1之间。其默认值为[.25, .5, .75], 它返回第25、50和75个百分位数。

include:它也是一个可选参数, 在描述DataFrame时包括数据类型列表。其默认值为无。

exclude:它也是一个可选参数, 在描述DataFrame时不包括数据类型列表。其默认值为无。

用法:DataFrame.describe(percentiles=None, include=None, exclude=None)
info = pd.DataFrame({'categorical': pd.Categorical(['s', 't', 'u']),
                   'numeric': [1, 2, 3], 'object': ['p', 'q', 'r']})
print(info.describe(),'\n')
          numeric
count      3.0
mean       2.0
std        1.0
min        1.0
25%        1.5
50%        2.0
75%        2.5
max        3.0

print(info.describe(include='all'),'\n')       
           categorical  numeric object
count            3      3.0      3
unique           3      NaN      3
top              u      NaN      p
freq             1      NaN      1
mean           NaN      2.0    NaN
std            NaN      1.0    NaN
min            NaN      1.0    NaN
25%            NaN      1.5    NaN
50%            NaN      2.0    NaN
75%            NaN      2.5    NaN
max            NaN      3.0    NaN

print(info.numeric.describe(),'\n')
count    3.0
mean     2.0
std      1.0
min      1.0
25%      1.5
50%      2.0
75%      2.5
max      3.0
Name: numeric, dtype: float64

print(info.describe(include=[np.number]),'\n')       
          numeric
count      3.0
mean       2.0
std        1.0
min        1.0
25%        1.5
50%        2.0
75%        2.5
max        3.0

print(info.describe(include=[np.object]),'\n')      
         object
count       3
unique      3
top         p
freq        1

print(info.describe(include=['category']),'\n')       
            categorical
count            3
unique           3
top              u
freq             1

print(info.describe(exclude=[np.number]),'\n')       
           categorical object
count            3      3
unique           3      3
top              u      p
freq             1      1

print(info.describe(exclude=[np.object]),'\n')       
           categorical  numeric
count            3      3.0
unique           3      NaN
top              u      NaN
freq             1      NaN
mean           NaN      2.0
std            NaN      1.0
min            NaN      1.0
25%            NaN      1.5
50%            NaN      2.0
75%            NaN      2.5
max            NaN      3.0

pandas.loc函数理解及用法

>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...      index=['cobra', 'viper', 'sidewinder'],
...      columns=['max_speed', 'shield'])
>>> df
            max_speed  shield
cobra               1       2
viper               4       5
sidewinder          7       8


Single label. Note this returns the row as a Series.
取出某列

>>> df.loc['viper']
max_speed    4
shield       5
Name: viper, dtype: int64


List of labels. Note using ``[[]]`` returns a DataFrame.
用双[[ ]]取出数据框


>>> df.loc[['viper', 'sidewinder']]
            max_speed  shield
viper               4       5
sidewinder          7       8


Single label for row and column
用行/列标签取某个元素


>>> df.loc['cobra', 'shield']
2


Slice with labels for row and single label for column. As mentioned
above, note that both the start and stop of the slice are included
多行标签,单列,注意是一个闭区间

>>> df.loc['cobra':'viper', 'max_speed']
cobra    1
viper    4
Name: max_speed, dtype: int64


Boolean list with the same length as the row axis
用跟行数相等长度的布尔值,来表示该行是否要取用

>>> df.loc[[False, False, True]]
            max_speed  shield
sidewinder          7       8


Conditional that returns a boolean Series
设定条件的返回

>>> df.loc[df['shield'] > 6]
            max_speed  shield
sidewinder          7       8


Conditional that returns a boolean Series with column labels specified


>>> df.loc[df['shield'] > 6, ['max_speed']]
            max_speed
sidewinder          7


Callable that returns a boolean Series
用可调用的方法返回的布尔序列来取用数据

>>> df.loc[lambda df: df['shield'] == 8]
            max_speed  shield
sidewinder          7       8


**Setting values**


Set value for all items matching the list of labels
对能匹配标签的的项设定值

>>> df.loc[['viper', 'sidewinder'], ['shield']] = 50
>>> df
            max_speed  shield
cobra               1       2
viper               4      50
sidewinder          7      50


Set value for an entire row
对整行设值

>>> df.loc['cobra'] = 10
>>> df
            max_speed  shield
cobra              10      10
viper               4      50
sidewinder          7      50


Set value for an entire column
对全列设值,注意要在逗号后,因为逗号前表示要设定的行的范围

>>> df.loc[:, 'max_speed'] = 30
>>> df
            max_speed  shield
cobra              30      10
viper              30      50
sidewinder         30      50


Set value for rows matching callable condition
对满足返回值的条件的行设定值

>>> df.loc[df['shield'] > 35] = 0
>>> df
            max_speed  shield
cobra              30      10
viper               0       0
sidewinder          0       0


**Getting values on a DataFrame with an index that has integer labels**


Another example using integers for the index
数字索引

>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...      index=[7, 8, 9], columns=['max_speed', 'shield'])
>>> df
   max_speed  shield
7          1       2
8          4       5
9          7       8


Slice with integer labels for rows. As mentioned above, note that both
the start and stop of the slice are included.


>>> df.loc[7:9]
   max_speed  shield
7          1       2
8          4       5
9          7       8


**Getting values with a MultiIndex**
用多项索引获值

A number of examples using a DataFrame with a MultiIndex


>>> tuples = [
...    ('cobra', 'mark i'), ('cobra', 'mark ii'),
...    ('sidewinder', 'mark i'), ('sidewinder', 'mark ii'),
...    ('viper', 'mark ii'), ('viper', 'mark iii')
... ]
>>> index = pd.MultiIndex.from_tuples(tuples)
>>> values = [[12, 2], [0, 4], [10, 20],
...         [1, 4], [7, 1], [16, 36]]
>>> df = pd.DataFrame(values, columns=['max_speed', 'shield'], index=index)
>>> df
                     max_speed  shield
cobra      mark i           12       2
           mark ii           0       4
sidewinder mark i           10      20
           mark ii           1       4
viper      mark ii           7       1
           mark iii         16      36


Single label. Note this returns a DataFrame with a single index.


>>> df.loc['cobra']
         max_speed  shield
mark i          12       2
mark ii          0       4


Single index tuple. Note this returns a Series.
元组索引,返回序列

>>> df.loc[('cobra', 'mark ii')]
max_speed    0
shield       4
Name: (cobra, mark ii), dtype: int64


Single label for row and column. Similar to passing in a tuple, this
returns a Series.
单个索引,返回序列

>>> df.loc['cobra', 'mark i']
max_speed    12
shield        2
Name: (cobra, mark i), dtype: int64


Single tuple. Note using ``[[]]`` returns a DataFrame.
返回数据框

>>> df.loc[[('cobra', 'mark ii')]]
               max_speed  shield
cobra mark ii          0       4


Single tuple for the index with a single label for the column
一个元组索引和一个标签,返回某个元素值

>>> df.loc[('cobra', 'mark i'), 'shield']
2


Slice from index tuple to single label
索引切片,返回数据框

>>> df.loc[('cobra', 'mark i'):'viper']
                     max_speed  shield
cobra      mark i           12       2
           mark ii           0       4
sidewinder mark i           10      20
           mark ii           1       4
viper      mark ii           7       1
           mark iii         16      36


Slice from index tuple to index tuple
元组索引:元素索引的切片,返回值同上一个

>>> df.loc[('cobra', 'mark i'):('viper', 'mark ii')]
                    max_speed  shield
cobra      mark i          12       2
           mark ii          0       4
sidewinder mark i          10      20
           mark ii          1       4
viper      mark ii          7       1

数据及解析源自官方文档

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335