zoukankan      html  css  js  c++  java
  • pandas库疑难问题---2、pandas切片操作

    pandas库疑难问题---2、pandas切片操作

    一、总结

    一句话总结:

    pandas的iloc方法可以用来切片,iloc方法具体使用细节和Python切片操作以及NumPy切片操作几乎一样
    iloc方法基本结构:iloc[start_index:end_index:step,start_index:end_index:step],逗号前面的start_index:end_index:step表示操作行,逗号后面的表示操作列

    二、pandas切片操作

    博客对应课程的视频位置:2、pandas切片操作-范仁义-读书编程笔记
    https://www.fanrenyi.com/video/39/379

    pandas的iloc方法可以用来切片,iloc方法具体使用细节和Python切片操作以及NumPy切片操作几乎一样

    pandas切片方法除了iloc(index+location)外,还有loc(location),这个后面会讲到的

    iloc方法基本结构:

    iloc[start_index:end_index:step,start_index:end_index:step]
    逗号前面的start_index:end_index:step表示操作行,逗号后面的表示操作列

    In [1]:
    import pandas as pd
    import numpy as np
    
    data = pd.DataFrame(np.arange(20).reshape((4,5)),index=list("ABCD"),columns=list("vwxyz"))
    print(data)
    
        v   w   x   y   z
    A   0   1   2   3   4
    B   5   6   7   8   9
    C  10  11  12  13  14
    D  15  16  17  18  19
    

    1、读取单个值

    In [2]:
    print(data.iloc[3])
    
    v    15
    w    16
    x    17
    y    18
    z    19
    Name: D, dtype: int32
    
    In [3]:
    print(data.iloc[-3])
    
    v    5
    w    6
    x    7
    y    8
    z    9
    Name: B, dtype: int32
    
    In [4]:
    print(data.iloc[2,3])
    
    13
    
    In [5]:
    print(data.iloc[-2,-3])
    
    12
    

    2、读取行

    In [6]:
    print(data.iloc[2::-1])
    
        v   w   x   y   z
    C  10  11  12  13  14
    B   5   6   7   8   9
    A   0   1   2   3   4
    
    In [7]:
    print(data.iloc[:3:2])
    
        v   w   x   y   z
    A   0   1   2   3   4
    C  10  11  12  13  14
    

    3、读取行和列

    In [8]:
    print(data.iloc[2::-1,-1:-3:-1])
    
        z   y
    C  14  13
    B   9   8
    A   4   3
    
    In [9]:
    print(data.iloc[:2,1:-1])
    
       w  x  y
    A  1  2  3
    B  6  7  8
    

    不连续行列

    In [10]:
    print(data.iloc[[1,3],[1,3]])
    
        w   y
    B   6   8
    D  16  18
    

    4、其它方式

    In [11]:
    print(data["x"])
    
    A     2
    B     7
    C    12
    D    17
    Name: x, dtype: int32
    
    In [12]:
    print(data.x)
    
    A     2
    B     7
    C    12
    D    17
    Name: x, dtype: int32
    
    In [ ]:
     
     
    博客对应系列课程视频位置:
    1、pandas打乱数据集-范仁义-读书编程笔记
    https://www.fanrenyi.com/video/39/360
    2、pandas切片操作-范仁义-读书编程笔记
    https://www.fanrenyi.com/video/39/379
    3、loc方法和iloc方法的区别-范仁义-读书编程笔记
    https://www.fanrenyi.com/video/39/380
    4、DataFrame类型转换成Numpy中ndarray-范仁义-读书编程笔记
    https://www.fanrenyi.com/video/39/381
     
    我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站: fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
    博主25岁,前端后端算法大数据人工智能都有兴趣。
    大家有啥都可以加博主联系方式(qq404006308,微信fan404006308)互相交流。工作、生活、心境,可以互相启迪。
    聊技术,交朋友,修心境,qq404006308,微信fan404006308
    26岁,真心找女朋友,非诚勿扰,微信fan404006308,qq404006308
    人工智能群:939687837

    作者相关推荐

  • 相关阅读:
    关于API微服务网关
    适用于企业的API管理平台
    简单的api测试
    Json,2020年api数据格式的Top 1
    API文档之团队协作
    如何进行API测试以提高程序质量
    API接口也要监控?
    春招实习_腾讯 wxg 一面 3.27 15:00
    春招实习_腾讯一面 & 二面_3.13
    春招实习_阿里一面
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13882383.html
Copyright © 2011-2022 走看看