您的位置:首页 > 编程语言 > Python开发

python进阶-14.pandas-排序-重复轴判断

2020-08-16 20:22 99 查看

文章目录

  • 3.6 重复的 轴 的 判断
  • 3.5排序

    • list.sort() 操作于原值
    • ndarray.sort() 操作于原值
    • np.sort(ndarray) 原值不变 ,返回新值

    pandas 排序

    • obj.sort_index(ascending = True) 默认升序

    • obj.sort_values(by=‘label’,axis=0,ascending=True,inplace=False)

    • by:str :表示根据 axis方向的索引名进行排序

    • axis:0/1 :表示轴

    • ascending:bool :默认True升序,Flase 是降序

    • inplace:false ;默认不在原值上排序,返回新值

    • 准备一个Series

    s1=Series(np.random.choice(range(1,7),6,replace=False)*10,index=list('adefcb'))
    s1
    a    30
    d    50
    e    10
    f    20
    c    40
    b    60
    dtype: int32
    • 排 索引
    s1.sort_index()
    a    30
    b    60
    c    40
    d    50
    e    10
    f    20
    dtype: int32
    s1.sort_values()
    e    10
    f    20
    a    30
    c    40
    d    50
    b    60
    dtype: int32
    s1.sort_values(ascending=False)
    b    60
    d    50
    c    40
    a    30
    f    20
    e    10
    dtype: int32
    • 对 DataFrame 排序
    df3 = DataFrame(
    np.random.randint(1,100,15).reshape(3,5) ,
    index = 'three/two/one'.split('/'),
    columns=list('baced')
    )
    df3
    b	a	c	e	d
    three	12	14	44	20	14
    two		75	56	36	54	94
    one		37	15	99	67	3
    • 排索引
    df3.sort_index(axis=0)
    b	a	c	e	d
    one		37	15	99	67	3
    three	12	14	44	20	14
    two		75	56	36	54	94
    df3.sort_index(axis=1)
    a	b	c	d	e
    three	14	12	44	14	20
    two		56	75	36	94	54
    one		15	37	99	3	67
    • 排值
    df3.sort_values(by='c',ascending=True,axis=0)
    b	a	c	e	d
    two		75	56	36	54	94
    three	12	14	44	20	14
    one		37	15	99	67	3
    • 按照 one 降序排序
    df3.sort_values(by='one',ascending=False,axis=1)
    c	e	b	a	d
    three	44	20	12	14	14
    two		36	54	75	56	94
    one		99	67	37	15	3
    ​

    模拟创建一个成绩表,语文,数学,英语。各科成绩有重复。

    names = '尺,寸,人,下,匕,卜,之,田,丫,乃,贝,井,工,几,女,巨,爪,火,了,方,木,中,寸,石,户,友,夫,不,可,主,又,丑,巾,口,电,门,术,儿,羊,丁,心,天,化,气,正,页,兄,伏,大,计'.split(',')
    df4 = DataFrame(
    {
    '语文':np.random.randint(90,100,50),
    '数学':np.random.randint(80,100,50),
    '英语':np.random.randint(60,100,50)
    },
    index = [np.random.choice(list('赵钱孙李周吴郑王'))+names.pop(np.random.randint(0,len(names))) for i in range(50)]
    ​
    )
    df4
    语文	数学	英语
    赵丑	97	99	78
    周电	93	89	93
    李女	98	83	76
    王夫	92	97	70
    ......
    郑友	91	84	67
    郑大	99	88	72
    对df4 按照 语文降序排序
    df4.sort_values(by='语文',ascending=False)
    语文	数学	英语
    郑大	99	88	72
    王口	99	82	64
    孙门	99	82	61
    王了	99	88	74
    吴木	99	96	93
    赵丫	99	81	92
    .......
    郑乃	90	95	86
    李石	90	94	94
    赵兄	90	97	89
    按照多个值排序 by = [ 列1, 列2,。。。。。。。]
    df4.sort_values(by=['语文','数学'],ascending=False)
    语文	数学	英语
    吴木	99	96	93
    王了	99	88	74
    郑大	99	88	72
    孙门	99	82	61
    王口	99	82	64
    .......
    李石	90	94	94
    吴又	90	91	95
    周工	90	83	74
    • 新增一列为总分
    df4.sum(axis=1)
    • df4[‘总分’]=df4.sum(axis=1)
    df4.insert(3,'总分',df4.sum(axis=1))
    df4
    语文	数学	英语	总分
    赵丑	97	99	78	274
    周电	93	89	93	275
    李女	98	83	76	257
    王夫	92	97	70	259
    孙下	98	84	87	269
    ......
    孙可	93	94	67	254
    王口	99	82	64	245
    郑友	91	84	67	242
    郑大	99	88	72	259
    • 筛选出英语大于 95分
    df4[df4['英语']>95]
    语文	数学	英语	总分
    王羊	98	92	98	288
    吴中	95	93	96	284

    3.6 重复的 轴 的 判断

    s6=Series(range(10),index=list('aaaabbbccc'))
    s6
    a    0
    a    1
    a    2
    a    3
    b    4
    b    5
    b    6
    c    7
    c    8
    c    9
    dtype: int64
    索引有重复,读值
    s6['a']
    a    0
    a    1
    a    2
    a    3
    dtype: int64
    Series.is_unique 判断 Series 的值 是否 唯一。
    s6.is_unique
    True
    • 可以用来判断索引对象是否唯一
    s6.index.is_unique
    False
    df4.index.is_unique
    True

    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: 
    相关文章推荐