2-Pandas索引--数据分析

举报
brucexiaogui 发表于 2021/12/30 01:42:39 2021/12/30
【摘要】 Pandas索引 import pandas as pd​df = pd.read_csv('C:/JupyterWork/data/titanic.csv') :5df['Age'][:5]0 22.01 38.02 26.03 35.04 35.0Name: Age, dtype: float641、指...

  
  1. Pandas索引
  2. import pandas as pd
  3. ​
  4. df = pd.read_csv('C:/JupyterWork/data/titanic.csv')
  5. :5
  6. df['Age'][:5]
  7. 0 22.0
  8. 1 38.0
  9. 2 26.0
  10. 3 35.0
  11. 4 35.0
  12. Name: Age, dtype: float64
  13. 1、指定显示两个列以上的数据
  14. :5
  15. df[['Age','Fare']][:5]
  16. Age Fare
  17. 0 22.0 7.2500
  18. 1 38.0 71.2833
  19. 2 26.0 7.9250
  20. 3 35.0 53.1000
  21. 4 35.0 8.0500
  22. 指定显示行索引具体的数据需要用到loc和iloc
  23. loc:用label来定位去获取数据
  24. iloc:用位置来定位获取数据
  25. 0
  26. df.iloc[0]
  27. PassengerId 1
  28. Survived 0
  29. Pclass 3
  30. Name Braund, Mr. Owen Harris
  31. Sex male
  32. Age 22
  33. SibSp 1
  34. Parch 0
  35. Ticket A/5 21171
  36. Fare 7.25
  37. Cabin NaN
  38. Embarked S
  39. Name: 0, dtype: object
  40. 0:5
  41. df.iloc[0:5]
  42. PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
  43. 0 1 0 3 Braund, Mr. Owen Harris male 22.0 1 0 A/5 21171 7.2500 NaN S
  44. 1 2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 0 PC 17599 71.2833 C85 C
  45. 2 3 1 3 Heikkinen, Miss. Laina female 26.0 0 0 STON/O2. 3101282 7.9250 NaN S
  46. 3 4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 0 113803 53.1000 C123 S
  47. 4 5 0 3 Allen, Mr. William Henry male 35.0 0 0 373450 8.0500 NaN S
  48. 在显示指定的行数据上,再次过滤显示指定列的数据
  49. 1:3]
  50. df.iloc[0:5,1:3]
  51. Survived Pclass
  52. 0 0 3
  53. 1 1 1
  54. 2 1 3
  55. 3 1 1
  56. 4 0 3
  57. df = df.set_index('Name')
  58. 用标签名称做定位就用loc
  59. 用一个数值定位,就用iloc
  60. df.loc['Braund, Mr. Owen Harris']
  61. df.loc['Braund, Mr. Owen Harris']
  62. PassengerId 1
  63. Survived 0
  64. Pclass 3
  65. Sex male
  66. Age 22
  67. SibSp 1
  68. Parch 0
  69. Ticket A/5 21171
  70. Fare 7.25
  71. Cabin NaN
  72. Embarked S
  73. Name: Braund, Mr. Owen Harris, dtype: object
  74. 用loc定位显示指定行中,指定列的数据
  75. df.loc['Braund, Mr. Owen Harris','Fare']
  76. df.loc['Braund, Mr. Owen Harris','Fare']
  77. 7.25
  78. 用loc定位具体的数据后,并修改数据
  79. df.loc['Braund, Mr. Owen Harris','Fare']=1000
  80. df.loc['Braund, Mr. Owen Harris','Fare']
  81. 1000.0
  82. boolean 类型索引
  83. df['Fare']>40
  84. Name
  85. Braund, Mr. Owen Harris True
  86. Cumings, Mrs. John Bradley (Florence Briggs Thayer) True
  87. Heikkinen, Miss. Laina False
  88. Futrelle, Mrs. Jacques Heath (Lily May Peel) True
  89. Allen, Mr. William Henry False
  90. Moran, Mr. James False
  91. McCarthy, Mr. Timothy J True
  92. Palsson, Master. Gosta Leonard False
  93. Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) False
  94. Nasser, Mrs. Nicholas (Adele Achem) False
  95. Sandstrom, Miss. Marguerite Rut False
  96. Bonnell, Miss. Elizabeth False
  97. Saundercock, Mr. William Henry False
  98. Andersson, Mr. Anders Johan False
  99. Vestrom, Miss. Hulda Amanda Adolfina False
  100. Hewlett, Mrs. (Mary D Kingcome) False
  101. Rice, Master. Eugene False
  102. Williams, Mr. Charles Eugene False
  103. Vander Planke, Mrs. Julius (Emelia Maria Vandemoortele) False
  104. Masselmani, Mrs. Fatima False
  105. Fynney, Mr. Joseph J False
  106. Beesley, Mr. Lawrence False
  107. McGowan, Miss. Anna "Annie" False
  108. Sloper, Mr. William Thompson False
  109. Palsson, Miss. Torborg Danira False
  110. Asplund, Mrs. Carl Oscar (Selma Augusta Emilia Johansson) False
  111. Emir, Mr. Farred Chehab False
  112. Fortune, Mr. Charles Alexander True
  113. O'Dwyer, Miss. Ellen "Nellie" False
  114. Todoroff, Mr. Lalio False
  115. ...
  116. Giles, Mr. Frederick Edward False
  117. Swift, Mrs. Frederick Joel (Margaret Welles Barron) False
  118. Sage, Miss. Dorothy Edith "Dolly" True
  119. Gill, Mr. John William False
  120. Bystrom, Mrs. (Karolina) False
  121. Duran y More, Miss. Asuncion False
  122. Roebling, Mr. Washington Augustus II True
  123. van Melkebeke, Mr. Philemon False
  124. Johnson, Master. Harold Theodor False
  125. Balkic, Mr. Cerin False
  126. Beckwith, Mrs. Richard Leonard (Sallie Monypeny) True
  127. Carlsson, Mr. Frans Olof False
  128. Vander Cruyssen, Mr. Victor False
  129. Abelson, Mrs. Samuel (Hannah Wizosky) False
  130. Najib, Miss. Adele Kiamie "Jane" False
  131. Gustafsson, Mr. Alfred Ossian False
  132. Petroff, Mr. Nedelio False
  133. Laleff, Mr. Kristo False
  134. Potter, Mrs. Thomas Jr (Lily Alexenia Wilson) True
  135. Shelley, Mrs. William (Imanita Parrish Hall) False
  136. Markun, Mr. Johann False
  137. Dahlberg, Miss. Gerda Ulrika False
  138. Banfield, Mr. Frederick James False
  139. Sutehall, Mr. Henry Jr False
  140. Rice, Mrs. William (Margaret Norton) False
  141. Montvila, Rev. Juozas False
  142. Graham, Miss. Margaret Edith False
  143. Johnston, Miss. Catherine Helen "Carrie" False
  144. Behr, Mr. Karl Howell False
  145. Dooley, Mr. Patrick False
  146. Name: Fare, Length: 891, dtype: bool
  147. 用bool类型帅选数据,只显示符合条件的数据
  148. :5
  149. df[df['Fare']>40][:5]
  150. PassengerId Survived Pclass Sex Age SibSp Parch Ticket Fare Cabin Embarked
  151. Name
  152. Braund, Mr. Owen Harris 1 0 3 male 22.0 1 0 A/5 21171 1000.0000 NaN S
  153. Cumings, Mrs. John Bradley (Florence Briggs Thayer) 2 1 1 female 38.0 1 0 PC 17599 71.2833 C85 C
  154. Futrelle, Mrs. Jacques Heath (Lily May Peel) 4 1 1 female 35.0 1 0 113803 53.1000 C123 S
  155. McCarthy, Mr. Timothy J 7 0 1 male 54.0 0 0 17463 51.8625 E46 S
  156. Fortune, Mr. Charles Alexander 28 0 1 male 19.0 3 2 19950 263.0000 C23 C25 C27 S
  157. 显示Sex为male的前5条数据
  158. df[df['Sex']== 'male'][:5]
  159. PassengerId Survived Pclass Sex Age SibSp Parch Ticket Fare Cabin Embarked
  160. Name
  161. Braund, Mr. Owen Harris 1 0 3 male 22.0 1 0 A/5 21171 1000.0000 NaN S
  162. Allen, Mr. William Henry 5 0 3 male 35.0 0 0 373450 8.0500 NaN S
  163. Moran, Mr. James 6 0 3 male NaN 0 0 330877 8.4583 NaN Q
  164. McCarthy, Mr. Timothy J 7 0 1 male 54.0 0 0 17463 51.8625 E46 S
  165. Palsson, Master. Gosta Leonard 8 0 3 male 2.0 3 1 349909 21.0750 NaN S
  166. 显示Sex为male数据,并且计算数据中age的平均值
  167. df.loc[df['Sex']=='male','Age'].mean()
  168. 30.72664459161148
  169. 统计数据中Age大于70的数量
  170. (
  171. (df['Age']>70).sum()
  172. 5

文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。

原文链接:brucelong.blog.csdn.net/article/details/80719026

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0)

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。