zoukankan      html  css  js  c++  java
  • 对dataframe中某一列进行计数

    本来是一项很简单的任务。。。但很容易忘记搞混。。所以还是记录一下

    方法一:

    df['col'].value_counts()

    方法二:

    groups = df.groupby('col')
    groups.size()  # 这里很容易就用上counts所以错误...

    对index进行更改:

    df2.index = df2.index.map(f) # f为函数

    按值排序

    基本语法:by='name' 指定按该行/列来排序; 默认ascending=True,升序排序;

    >>> df3 = pd.DataFrame(np.random.randint(0,10,(4, 3)), columns=list('bde'), index=range(4))
    >>> df3
       b  d  e
    0  3  6  7
    1  7  7  2
    2  4  5  4
    3  1  0  3

    # 默认axis=0,对col_name列进行排序,该列中每个值对应的行也跟着变动;

    df.sort_index(by='col_name')

    >>> df3.sort_values(by='b')
       b  d  e
    3  1  0  3
    0  3  6  7
    2  4  5  4
    1  7  7  2

    # 对第row_name行进行排序,该行中每个值相应的列也跟着变动,ascending=False 降序

    df.sort_index(by='row_name', axis=1, ascending=False)

    >>> df3.sort_values(by=3, ascending=False, axis=1)
       e  b  d
    0  7  3  6
    1  2  7  7
    2  4  4  5
    3  3  1  0
  • 相关阅读:
    Prometheus对标签的处理
    Promethueus常用函数
    jenkins容器化docker-compose
    k8s常用命令
    k8s网络笔记
    动态更新已经存在配置
    prometheus远程写调优参数说明
    IndiaHacks 2016
    Codeforces Round #344 (Div. 2) Messager KMP的应用
    HDU1711 KMP的应用
  • 原文地址:https://www.cnblogs.com/cymwill/p/10555148.html
Copyright © 2011-2022 走看看