zoukankan      html  css  js  c++  java
  • pandas库笔记

    本笔记为自学笔记

    1、pandas.DataFrame()

    一种保存矩阵的数据格式

    创建DataFrame的方式有多种

    例子(1)、

    grades_df = pd.DataFrame(
        data={'exam1': [43, 81, 78, 75, 89, 70, 91, 65, 98, 87],
              'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]},
        index=['Andre', 'Barry', 'Chris', 'Dan', 'Emilio', 
               'Fred', 'Greta', 'Humbert', 'Ivan', 'James']
    )
    print(grades_df)
             exam1  exam2
    Andre       43     24
    Barry       81     63
    Chris       78     56
    Dan         75     56
    Emilio      89     67
    Fred        70     51
    Greta       91     79
    Humbert     65     46
    Ivan        98     72
    James       87     60

    例子(2)、

    grades_df1 = pd.DataFrame(
        [[43, 81, 78, 75, 89, 70, 91, 65, 98, 87],[24, 63, 56, 56, 67, 51, 79, 46, 72, 60]],
        columns=['Andre', 'Barry', 'Chris', 'Dan', 'Emilio',
               'Fred', 'Greta', 'Humbert', 'Ivan', 'James']
    )
    print(grades_df1)
       Andre  Barry  Chris  Dan  Emilio  Fred  Greta  Humbert  Ivan  James
    0     43     81     78   75      89    70     91       65    98     87
    1     24     63     56   56      67    51     79       46    72     60

     参考:https://blog.csdn.net/tefuirnever/article/details/93708964

    2、DataFrame().apply()

    dataframe.apply(function,axis)对一行或一列做出一些操作(axis=1则为对某一列进行操作,此时,apply函数每次将dataframe的一行传给function,然后获取返回值,将返回值放入一个series)

    fuction是对数据的处理函数

    eg: 

    df=pd.DataFrame(np.random.randn(4,3),columns=list('bde'),index=['utah','ohio','texas','oregon'])  
    f=lambda x:x.max()-x.min()  
    
    #默认情况下会以列为单位,分别对列应用函数  
    t1=df.apply(f)  
    print(t1)  
    t2=df.apply(f,axis=1)  
    print(t2) 

    参考:https://msd.misuland.com/pd/3629960913806691442

    3、pandas.Serise()

    Series 是一个带有 名称 和索引的一维数组,既然是数组,肯定要说到的就是数组中的元素类型,在 Series 中包含的数据类型可以是整数、浮点、字符串、Python对象等。

    参考:https://www.bbsmax.com/A/Vx5M6kX7dN/

    4、pandas.cut

    pandas.cut用来把一组数据分割成离散的区间。比如有一组年龄数据,可以使用pandas.cut将年龄数据分割成不同的年龄段并打上

    参考:https://www.cnblogs.com/sench/p/10128216.html

    5、pandas.qcut()

    按照区间转换数据值,pandas.cut的变种

    param1:DataFrame数据

    param2:区间比例

    param3:转换后的表示

    eg:

    grades_df = pd.DataFrame(
        data={'exam1': [43, 81, 78, 75, 89, 70, 91, 65, 98, 87],
              'exam2': [24, 63, 56, 56, 67, 51, 79, 46, 72, 60]},
        index=['Andre', 'Barry', 'Chris', 'Dan', 'Emilio', 
               'Fred', 'Greta', 'Humbert', 'Ivan', 'James']
    )
    def convert_grades_curve(exam_grades):
        return pd.qcut(exam_grades, [0, 0.1, 0.2, 0.5, 0.8, 1], labels=['E', 'D', 'C', 'B', 'A'])
    
    t_data = pd.qcut(grades_df, [0, 0.1, 0.2, 0.5, 0.8, 1], labels=['E', 'D', 'C', 'B', 'A'])

    # convert_grades_curve 通过apply默认传参
    print(grades_df.apply(convert_grades_curve))
  • 相关阅读:
    lampp、xampp安装文档
    tomcat下配置https方式
    1.6:怎么学习Linux
    1.5:linux的应用领域
    1.3:linux的优点和缺点
    1.4:Linux发行版本详解
    1.2:liunx和unix的区别
    1.1:Linux系统简介
    mysql中获取表名&字段名的查询语句
    kettle组件-输出
  • 原文地址:https://www.cnblogs.com/fclbky/p/12377226.html
Copyright © 2011-2022 走看看