zoukankan      html  css  js  c++  java
  • pandas_一维数组与常用操作

    # 一维数组与常用操作
    import pandas as pd
    
    # 设置输出结果列对齐
    pd.set_option('display.unicode.ambiguous_as_wide',True)
    pd.set_option('display.unicode.east_asian_width',True)
    
    # 创建 从 0 开始的非负整数索引
    s1 = pd.Series(range(1,20,5))
    '''
    0     1
    1     6
    2    11
    3    16
    dtype: int64
    '''
    # 使用字典创建 Series 字典的键作为索引
    s2 = pd.Series({'语文':95,'数学':98,'Python':100,'物理':97,'化学':99})
    '''
    语文       95
    数学       98
    Python    100
    物理       97
    化学       99
    dtype: int64
    '''
    # 使用索引下标进行修改
    # 修改 Series 对象的值
    s1[3] = -17
    '''
    0     1
    1     6
    2    11
    3   -17
    dtype: int64
    '''
    s2['语文'] = 94
    '''
    语文       94
    数学       98
    Python    100
    物理       97
    化学       99
    dtype: int64
    '''
    # 查看 s1 的绝对值
    abs(s1)
    '''
    0     1
    1     6
    2    11
    3    17
    dtype: int64
    '''
    # 将 s1 所有的值都加 5、使用加法时,对所有元素都进行
    s1 + 5
    '''
    0     6
    1    11
    2    16
    3   -12
    dtype: int64
    '''
    # 在 s1 的索引下标前加入参数值
    s1.add_prefix(2)
    '''
    20     1
    21     6
    22    11
    23   -17
    dtype: int64
    '''
    # s2 数据的直方图
    s2.hist()
    
    # 每行索引后面加上 hany
    s2.add_suffix('hany')
    '''
    语文hany       94
    数学hany       98
    Pythonhany    100
    物理hany       97
    化学hany       99
    dtype: int64
    '''
    # 查看 s2 中最大值的索引
    s2.argmax()
    # 'Python'
    
    # 查看 s2 的值是否在指定区间内
    s2.between(90,100,inclusive = True)
    '''
    语文      True
    数学      True
    Python    True
    物理      True
    化学      True
    dtype: bool
    '''
    # 查看 s2 中 97 分以上的数据
    s2[s2 > 97]
    '''
    数学       98
    Python    100
    化学       99
    dtype: int64
    '''
    # 查看 s2 中大于中值的数据
    s2[s2 > s2.median()]
    '''
    Python    100
    化学       99
    dtype: int64
    '''
    # s2 与数字之间的运算,开平方 * 10 保留一位小数
    round((s2**0.5)*10,1)
    '''
    语文       97.0
    数学       99.0
    Python    100.0
    物理       98.5
    化学       99.5
    dtype: float64
    '''
    # s2 的中值
    s2.median()
    # 98.0
    
    # s2 中最小的两个数
    s2.nsmallest(2)
    '''
    语文    94
    物理    97
    dtype: int64
    '''
    # s2 中最大的两个数
    s2.nlargest(2)
    '''
    Python    100
    化学       99
    dtype: int64
    '''
    # Series 对象之间的运算,对相同索引进行计算,不是相同索引的使用 NaN
    pd.Series(range(5)) + pd.Series(range(5,10))
    '''
    0     5
    1     7
    2     9
    3    11
    4    13
    dtype: int64
    '''
    # 对 Series 对象使用匿名函数
    pd.Series(range(5)).pipe(lambda x,y,z :(x**y)%z,2,5)
    '''
    0    0
    1    1
    2    4
    3    4
    4    1
    dtype: int64
    '''
    pd.Series(range(5)).pipe(lambda x:x+3)
    '''
    0    3
    1    4
    2    5
    3    6
    4    7
    dtype: int64
    '''
    pd.Series(range(5)).pipe(lambda x:x+3).pipe(lambda x:x*3)
    '''
    0     9
    1    12
    2    15
    3    18
    4    21
    dtype: int64
    '''
    # 对 Series 对象使用匿名函数
    pd.Series(range(5)).apply(lambda x:x+3)
    '''
    0    3
    1    4
    2    5
    3    6
    4    7
    dtype: int64
    '''
    # 查看标准差
    pd.Series(range(0,5)).std()
    # 1.5811388300841898
    
    # 查看无偏方差
    pd.Series(range(0,5)).var()
    # 2.5
    
    # 查看无偏标准差
    pd.Series(range(0,5)).sem()
    # 0.7071067811865476
    
    # 查看是否存在等价于 True 的值
    any(pd.Series([3,0,True]))
    # True
    
    # 查看是否所有的值都等价于 True
    all(pd.Series([3,0,True]))
    # False

    2020-05-07

  • 相关阅读:
    文字
    <script type="text/x-template"> 模板
    防xss攻击
    url
    symmfony
    composer
    header 和http状态码
    bootstrap
    linux的设置ip连接crt,修改主机名,映射,建文件
    Centos上传下载小工具lrzsz
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12844790.html
Copyright © 2011-2022 走看看