• Pandas初级认识


    深入浅出Pandas

    Series - 数据系列 - 一维数据
    DataFrame - 数据窗、数据框、数据表 - 二维数据
    Index - 索引 - 为Series和DataFrame提供索引服务

    scores = np.random.randint(60, 101, (5, 3))
    scores
    array([[ 62,  80,  78],
           [ 91,  71,  77],
           [ 99,  71, 100],
           [ 96,  77,  84],
           [ 73,  94,  99]])
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    通过二维数组构造DataFrame对象

    df1 = pd.DataFrame(
        data=scores,
        columns=['Verbal', 'Math', 'English'],
        index=np.arange(1001, 1006)
    )
    df1
    Verbal	Math	English
    1001	62	80	78
    1002	91	71	77
    1003	99	71	100
    1004	96	77	84
    1005	73	94	99
    data = {
       
        'Verbal': scores[:, 0],
        'Math': scores[:, 1],
        'English': scores[:, 2]
    }
    data
    {
       'Verbal': array([62, 91, 99, 96, 73]),
     'Math': array([80, 71, 71, 77, 94]),
     'English': array([ 78,  77, 100,  84,  99])}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    通过字典来创建DataFrame对象

    df2 = pd.DataFrame(data=data, index=np.arange(1001, 1006))
    df2
    Verbal	Math	English
    1001	62	80	78
    1002	91	71	77
    1003	99	71	100
    1004	96	77	84
    1005	73	94	99
    import sys
    ​
    sys.getdefaultencoding()
    'utf-8'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    读取CSV文件的数据创建DataFrame对象

    df3 = pd.read_csv(
        '2018年北京积分落户数据.csv',
        index_col='id',  # 设置索引列(哪个列充当索引)
        # encoding='utf-8',  # 设置字符编码方式
        # usecols=['id', 'name', 'company', 'score'],  # 设置需要加载的列
        # nrows=20,  # 设置加载的行数
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    【漏洞复现】WebLogic
    react-redux基本使用
    电脑改时间导致的路由器网络问题
    TypeScript学习大纲
    Mac电脑专业音乐制作Logic Pro X中文
    集成学习
    【洛谷】P1114 “非常男女”计划
    第19章_瑞萨MCU零基础入门系列教程之RTC
    Docker 快速部署 SpringBoot2 项目
    固态硬盘分区详细指南
  • 原文地址:https://blog.csdn.net/m0_58239511/article/details/128141429