程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

python面试题(7)

python杂谈(0)

标签  

python面试题(7)

python电子书(56)

日期归档  

Pandas知识点总结

发布于2019-08-22 17:50     阅读(397)     评论(0)     点赞(23)     收藏(2)


Pandas基于Numpy构建

Pandas最初被作为金融数据分析而开发出来,因而Pandas为时间序列分析提供了很好的支持

安装:cmd窗口中输入

pip install pandas

使用时,要先导入

import pandas as pd

1.Pandas中的数据结构 

1-1.Series----序列

  1. from pandas import Series
  2. # 1-创建Series
  3. obj = Series([4, 7, -5, 3])
  4. print(obj)
  5. 输出结果:(左边的是index,右边的是values)
  6. 0 4
  7. 1 7
  8. 2 -5
  9. 3 3
  10. dtype: int64
  11. # 2-获取index
  12. print(obj.index)
  13. 输出结果:
  14. RangeIndex(start=0, stop=4, step=1)
  15. # 3-获取values
  16. print(obj.values)
  17. 输出结果:
  18. [ 4 7 -5 3]
  19. # 4-可以指定索引---(例如将索引指定为日期)
  20. obj = Series([4, 7, -5, 3], index=['3/1', '3/2', '3/3', '3/4'])
  21. print(obj)
  22. 输出结果:
  23. 3/1 4
  24. 3/2 7
  25. 3/3 -5
  26. 3/4 3
  27. dtype: int64
  28. # 5-获取指定索引的值
  29. print(obj['3/3'])
  30. # 6-获取指定条件的值(如:获得大于0的值)
  31. print(obj[obj>0])
  32. # 7-in判断是否存在
  33. print('3/3' in obj)----True
  34. print('3/6' in obj)----False
  35. # 8-通过字典创建Series
  36. dict1 = {'3/1': 4, '3/2': 7, '3/3': -5, '3/4': 3}
  37. print(dict1)
  38. r = Series(dict1)
  39. print(r)

1-2.DataFrame---数据框

  1. from pandas import DataFrame
  2. # 1-创建数据框(直接传入一个由等长列表或numpy数组组成的字典)
  3. # 先定义2个列表
  4. position = ['产品经理', '数据分析师', 'UI', '产品经理', '开发']
  5. company = ['百度', '腾讯', '阿里', '网易', '京东']
  6. df = DataFrame([position, company]) # 将两组数据合成一个表格
  7. print(df)
  8. 输出结果:
  9. 0 1 2 3 4
  10. 0 产品经理 数据分析师 UI 产品经理 开发
  11. 1 百度 腾讯 阿里 网易 京东
  12. # 2-将上述列子的第一行作为第一列
  13. df = DataFrame([position, company]).T #---加.T
  14. print(df)
  15. 输出结果:
  16. 0 1
  17. 0 产品经理 百度
  18. 1 数据分析师 腾讯
  19. 2 UI 阿里
  20. 3 产品经理 网易
  21. 4 开发 京东
  22. # 3-columns指定列名
  23. df.columns = ['职位名', '公司名']
  24. print(df)
  25. 输出结果:
  26. 职位名 公司名
  27. 0 产品经理 百度
  28. 1 数据分析师 腾讯
  29. 2 UI 阿里
  30. 3 产品经理 网易
  31. 4 开发 京东
  32. # 4-index指定行名
  33. df.index = ['a', 'b', 'c', 'd', 'e']
  34. print(df)
  35. 输出结果:
  36. 职位名 公司名
  37. a 产品经理 百度
  38. b 数据分析师 腾讯
  39. c UI 阿里
  40. d 产品经理 网易
  41. e 开发 京东
  42. # 5-1-重置index
  43. print(df.reset_index(drop=True))
  44. # 5-2-重置index,并保存之前的索引
  45. print(df.reset_index())
  46. # 6-显示前面n行数据
  47. print(df.head(n))
  48. # 7-显示后面n行数据
  49. print(df.tail(n))
  50. # 8-获取某一列的值
  51. # 8-1-方式1
  52. r = df["职位名"]
  53. print(r)
  54. # 8-2-方式2
  55. r = df.职位名
  56. print(r)
  57. # 9-获取某一行的值
  58. # 9-1-方式1
  59. r = df.loc["c"]
  60. print(r)
  61. # 方式2
  62. r = df.iloc[3]
  63. print(r)

2-Pandas中一些常用的功能

  1. import numpy as np
  2. from pandas import Series
  3. from pandas import DataFrame
  4. # 创建一个数据框
  5. # 先创建一个4x4的数组
  6. arr = np.arange(16).reshape((4, 4))
  7. # 1-指定行名和列名
  8. df = DataFrame(data=arr, index=['a', 'b', 'c', 'd'], columns=['one', 'two', 'three', 'four'])
  9. print(df)
  10. 输出结果:
  11. one two three four
  12. a 0 1 2 3
  13. b 4 5 6 7
  14. c 8 9 10 11
  15. d 12 13 14 15
  16. # 2-删除某行
  17. r = df.drop('b')
  18. print(r)
  19. 输出结果:
  20. one two three four
  21. a 0 1 2 3
  22. c 8 9 10 11
  23. d 12 13 14 15
  24. # 3-删除某列
  25. r = df.drop('two', axis=1) # axis=1表示按列执行
  26. print(r)
  27. 输出结果:
  28. one three four
  29. a 0 2 3
  30. b 4 6 7
  31. c 8 10 11
  32. d 12 14 15
  33. # 4-查找x列,值为y的记录(将显示一行的值)
  34. r = df[df["four"] == 7] # four列,值为7
  35. print(r)
  36. 输出结果:
  37. one two three four
  38. b 4 5 6 7
  39. # 5-唯一值 unique
  40. # 先创建一个序列
  41. obj = Series([1, 2, 2, 3, 3, 4, 5, 5, 5])
  42. print(obj)
  43. r = obj.unique()
  44. print(r)
  45. 输出结果:
  46. [1 2 3 4 5]
  47. # 6-频率统计,value_counts(),频率从高到低排序
  48. r = obj.value_counts()
  49. print(r)
  50. 输出结果:
  51. 5 3
  52. 3 2
  53. 2 2
  54. 4 1
  55. 1 1
  56. dtype: int64
  57. # 7-频率统计,value_counts(),不排序
  58. r = obj.value_counts(sort=False)
  59. print(r)
  60. 输出结果:
  61. 1 1
  62. 2 2
  63. 3 2
  64. 4 1
  65. 5 3
  66. dtype: int64

3.Pandas中常用的数学和统计函数

  1. 创建一个数据框
  2. arr = np.arange(16).reshape((4, 4))
  3. df = DataFrame(data=arr, index=['a', 'b', 'c', 'd'], columns=['one', 'two', 'three', 'four'])
  4. print(df)
  5. 输出结果:
  6. one two three four
  7. a 0 1 2 3
  8. b 4 5 6 7
  9. c 8 9 10 11
  10. d 12 13 14 15
  11. # 1-describe,描述性统计分析
  12. r = df.describe()
  13. print(r)
  14. 输出结果:
  15. one two three four
  16. count 4.000000 4.000000 4.000000 4.000000
  17. mean 6.000000 7.000000 8.000000 9.000000
  18. std 5.163978 5.163978 5.163978 5.163978
  19. min 0.000000 1.000000 2.000000 3.000000
  20. 25% 3.000000 4.000000 5.000000 6.000000
  21. 50% 6.000000 7.000000 8.000000 9.000000
  22. 75% 9.000000 10.000000 11.000000 12.000000
  23. max 12.000000 13.000000 14.000000 15.000000
  24. # 2-求和--对每一列
  25. r = df.sum()
  26. print(r)
  27. 输出结果:
  28. one 24
  29. two 28
  30. three 32
  31. four 36
  32. dtype: int64
  33. # 3-均值mean()--对每一列
  34. r = df.mean()
  35. print(r)
  36. 输出结果:
  37. one 6.0
  38. two 7.0
  39. three 8.0
  40. four 9.0
  41. dtype: float64
  42. # 4-累计求和cumsum--对行
  43. r = df.cumsum()
  44. print(r)
  45. 输出结果:
  46. one two three four
  47. a 0 1 2 3
  48. b 4 6 8 10
  49. c 12 15 18 21
  50. d 24 28 32 36
  51. # 5-求每一列的最大值
  52. r = df.max()
  53. print(r)
  54. 输出结果:
  55. one 12
  56. two 13
  57. three 14
  58. four 15
  59. dtype: int32
  60. # 创建一个数据框
  61. position = ['产品经理', '数据分析师', 'UI', '产品经理', '开发']
  62. company = ['百度', '腾讯', '阿里', '网易', '京东']
  63. df = DataFrame([position, company]).T
  64. print(df)
  65. # 6-非数值型数据的描述性统计分析
  66. r = df.describe()
  67. print(r)
  68. 输出结果:
  69. 0 1
  70. count 5 5
  71. unique 4 5
  72. top 产品经理 腾讯
  73. freq 2 1

 



所属网站分类: 技术文章 > 博客

作者:熊猫烧香

链接:https://www.pythonheidong.com/blog/article/53312/8c72b1e02f5fe3d5310e/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

23 0
收藏该文
已收藏

评论内容:(最多支持255个字符)