感谢博主,转载自:

https://blog.csdn.net/qq_38328378/article/details/81166518

http://www.datastudy.cc/article/ec8c50baa8fd93ea85432eb85fb34eee

目录

技能1、选择一列

技能2、选择多列

技能3、根据一个行索引,选择出一行

技能4、根据一个行序号,选择出从开始到这个序号的行

技能5、根据两个行序号,选择出从第一个序号到第二个序号的行

技能7、根据一个列序号,选择出从开始列到这个序号的所有列

技能8、条件过滤

技能9、根据行字符串索引,进行行选择

技能10、根据行索引/行位置,列名/列位置,进行具体位置的值选


Pandas中行列选择的十大技能

今天,我们来学习一下,Pandas中的关于行列选择的十大技能,这些技能,绝对是你使用Pandas的过程中,需要用到的,因为,你肯定也想像Excel一样,任性地操作Python中的数据框。

先来导入我们的演示数据,这里你直接复制执行就可以了。

# import the pandas module import pandas as pd# Create an example dataframe about a fictional army raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons','Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']
} df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters', 'origin']) df = df.set_index('origin') df.head()
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
California Nighthawks 1st 52 42 957 5 2 0 24
Texas Nighthawks 2nd 25 2 1099 62 3 1 31
Florida Nighthawks 2nd 616 2 1400 26 3 1 2
Maine Dragoons 1st 43 4 1592 73 2 0

3

技能1、选择一列

df['size']

输出结果:

origin
Arizona       1045
California     957
Texas         1099
Florida       1400
Maine         1592
Iowa          1006
Alaska         987
Washington     849
Oregon         973
Wyoming       1005
Louisana      1099
Georgia       1523
Name: size, dtype: int64

技能2、选择多列

df[['size', 'veterans']]
  size veterans
origin    
Arizona 1045 1
California 957 5
Texas 1099 62
Florida 1400 26
Maine 1592 73
Iowa 1006 37
Alaska 987 949
Washington 849 48
Oregon 973 48
Wyoming 1005 435
Louisana 1099 63
Georgia 1523 345

    技能3、根据一个行索引,选择出一行

# Select all rows with the index label "Arizona"
df.loc[:'Arizona']
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4

技能4、根据一个行序号,选择出从开始到这个序号的行

# Select every row up to 3
df.iloc[:2]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
California Nighthawks 1st 52 42 957 5 2 0 24

技能5、根据两个行序号,选择出从第一个序号到第二个序号的行

    df.iloc[1:2]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
California Nighthawks 1st 52 42 957 5 2 0 24

技能6、根据一个行序号,选择出从这个行序号开始到结束的行

df.iloc[2:]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Texas Nighthawks 2nd 25 2 1099 62 3 1 31
Florida Nighthawks 2nd 616 2 1400 26 3 1 2
Maine Dragoons 1st 43 4 1592 73 2 0 3
Iowa Dragoons 1st 234 7 1006 37 1 1 4
Alaska Dragoons 2nd 523 8 987 949 2 0 24
Washington Dragoons 2nd 62 3 849 48 3 1 31
Oregon Scouts 1st 62 4 973 48 2 0 2
Wyoming Scouts 1st 73 7 1005 435 1 0 3
Louisana Scouts 2nd 37 8 1099 63 2 1 2
Georgia Scouts 2nd 35 9 1523 345 3 1 3

技能7、根据一个列序号,选择出从开始列到这个序号的所有列

 # Select the first 2 columnsdf.iloc[:,:2]
  regiment company
origin    
Arizona Nighthawks 1st
California Nighthawks 1st
Texas Nighthawks 2nd
Florida Nighthawks 2nd
Maine Dragoons 1st
Iowa Dragoons 1st
Alaska Dragoons 2nd
Washington Dragoons 2nd
Oregon Scouts 1st
Wyoming Scouts 1st
Louisana Scouts 2nd
Georgia Scouts 2nd

技能8、条件过滤

 # Select rows where df.deaths is greater than 50df[df['deaths'] > 50]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
California Nighthawks 1st 52 42 957 5 2 0 24
Florida Nighthawks 2nd 616 2 1400 26 3 1 2
Iowa Dragoons 1st 234 7 1006 37 1 1 4
Alaska Dragoons 2nd 523 8 987 949 2 0 24
Washington Dragoons 2nd 62 3 849 48 3 1 31
Oregon Scouts 1st 62 4 973 48 2 0 2
Wyoming Scouts 1st 73 7 1005 435 1 0 3
# Select rows where df.deaths is greater than 500 or less than 50
df[(df['deaths'] > 500) | (df['deaths'] < 50)]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
Texas Nighthawks 2nd 25 2 1099 62 3 1 31
Florida Nighthawks 2nd 616 2 1400 26 3 1 2
Maine Dragoons 1st 43 4 1592 73 2 0 3
Alaska Dragoons 2nd 523 8 987 949 2 0 24
Louisana Scouts 2nd 37 8 1099 63 2 1 2
Georgia Scouts 2nd 35 9 1523 345 3 1 3
# Select all the regiments not named "Dragoons"
df[~(df['regiment'] == 'Dragoons')]
  regiment company deaths battles size veterans readiness armored deserters
origin                  
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
California Nighthawks 1st 52 42 957 5 2 0 24
Texas Nighthawks 2nd 25 2 1099 62 3 1 31
Florida Nighthawks 2nd 616 2 1400 26 3 1 2
Oregon Scouts 1st 62 4 973 48 2 0 2
Wyoming Scouts 1st 73 7 1005 435 1 0 3
Louisana Scouts 2nd 37 8 1099 63 2 1 2
Georgia Scouts 2nd 35 9 1523 345 3 1 3

技能9、根据行字符串索引,进行行选择

    # Select the rows called Texas and Arizonadf.ix[['Arizona', 'Texas']]
  regiment company deaths battles size veterans readiness armored deserters
Arizona Nighthawks 1st 523 5 1045 1 1 1 4
Texas Nighthawks 2nd 25 2 1099 62 3 1 31

技能10、根据行索引/行位置,列名/列位置,进行具体位置的值选

# Select the third cell in the row named Arizona
df.ix['Arizona', 'deaths']

523

# Select the third cell in the row named Arizona
df.ix['Arizona', 2]

523

# Select the third cell down in the column named deaths
df.ix[2, 'deaths']

25

Pandas选择行列的十大技能相关推荐

  1. 项目经理必备的项目管理十大技能

    所谓「事事皆项目」,项目管理知识体系具有普遍性应用特征,不管什么行业.无论什么做什么事,都可以应用项目管理,从建筑行业.到律师行业.软件行业--甚至可以这么说,如果你能掌握项目管理知识体系,你做事.无 ...

  2. ibm cloud_使IBM Cloud成为您选择平台的十大理由

    developerWorks发布了许多有关如何使用IBM Cloud服务的技术内容 . 在本文中,我将为您所见过的所有技术内容添加一些变化,并为您选择IBM Cloud作为您选择的云平台的十大理由. ...

  3. 未来十大技能职场吃香

    一个人掌握何种技能取决于他的兴趣.能力和聪明程度,也取决于他所能支配的资源以及制定的事业目标,拥有过硬技能的人有更多的工作机会.但是,由于经济发展前景不确定,掌握对你的事业有所帮助的技能显得尤为重要. ...

  4. SAP License:求职最受欢迎的十大技能

    一个人掌握何种技能取决于他的兴趣.能力和聪明程度,也取决于他所能支配的资源以及制定的事业目标,拥有过硬技能的人有更多的工作机会.但是,由于经济发展前景不确定,掌握对你的事业有所帮助的技能显得尤为重要. ...

  5. 前端开发人员所必备的十大技能

    2019独角兽企业重金招聘Python工程师标准>>> 前端开发的工作是要如何将自己的设计应用在网络上.它不同于网页设计,更关注于该网站将如何交互和浏览.前端开发布局,按钮和主要界面 ...

  6. 详解Linux运维工程师应具备的十大技能

    Linux系统如果是学习可以选用Redhat或CentOS,特别是CentOS在企业中用得最多,当然还会有其它版本的,但学习者还是以这2个版本学习就行,因为这两个版本都是兄弟,没区别的,有空可以再研究 ...

  7. java 15k_月薪15K的Java工程师必备的十大技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...

  8. 百度推广创意怎么写?百度推广创意十大秘籍

    "百度推广"里面的搜索推广(俗称百度竞价)"很黄很暴力",大家是知道的,竞价的成功与否不单单是关键字的选取,一个好的创意也起到相符相承的效果.下面由seo_ma ...

  9. 集成灶哪个品牌质量好,2022年集成灶十大名牌排行榜揭晓

    作为新兴行业的代表,集成灶已经开始走进千家万户的生活,凭借其超高吸烟效果.节省空间.节能低耗环保的优势,收获大批消费者的喜爱和青睐.那么集成灶品牌产品那么多,集成灶哪个品牌质量好?今天就为大家介绍20 ...

最新文章

  1. NeurIPS 2019公布获奖论文!新增杰出新方向奖项,微软华裔研究员斩获经典论文...
  2. [基础]iOS 可视化编程(全系列)
  3. SAP Cloud Platform integration上创建一个最简单的iFlow
  4. LINUX内核完全注释
  5. CSS Framework 960 Grid System (收)
  6. 关于markdown
  7. Centos下安装X Window+GNOME Desktop+FreeNX
  8. 11种刷新按钮的方法
  9. python习题错误整理(一)
  10. linux安装启动svn
  11. 台达DOP-B触摸屏通过MODBUS RTU通讯4台台达M变频器.含触摸屏程序,接线图和变频器参数设定
  12. 数据库实验一实验报告
  13. 静态HTML网页设计作品 HTML5+CSS大作业——个人网页设计(7页)
  14. 身边的一个可爱女程序媛
  15. 六、线性方程组求解--Jacobi和Gauss-Seidel迭代求解
  16. 机器学习——马尔可夫模型及马尔可夫决策过程(MDP)
  17. [效率提升]webstorm配置Prettier:代码自动格式,格式化时清除空行,修改使用代码模板
  18. BL5372 RTC linux驱动
  19. plotly入门(vue项目中)
  20. 国际物流公司有哪些公司呢?

热门文章

  1. 在虚幻引擎 4 中处理内存泄漏问题
  2. iOS 的内存管理和虚拟内存机制具体是怎么运作的?
  3. C# 中执行 msi 安装
  4. 周报(2017.3.19-3.16)
  5. tomcat配置虚拟目录的方法
  6. HTC S710D(电信版)G11解锁,成功ROOT
  7. 利用圆解一元二次方程
  8. android游戏开发框架libgdx的使用(四)--舞台和演员
  9. cesium获取模型高度_Cesium中地形数据的加载
  10. 多线程处理list_跟大佬聊天,被反问Redis6的多线程真的能提高性能吗?