python三大库之---pandas(二)

打印 上一主题 下一主题

主题 1851|帖子 1851|积分 5553

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
python三大库之—pandas(二)


  

六,函数

6.1、常用的统计学函数

函数名称形貌说明count()统计某个非空值的数目sum()求和mean()求均值median()求中位数std()求标准差min()求最小值max()求最大值abs()求绝对值prod()求全部数值的乘积 numpy的方差默认为总体方差,pandas默认为样本方差
  1. #常用的统计学函数
  2. data = {
  3.     'A': [1, 2, 3, 4],
  4.     'B': [5, 6, 7, 8],
  5.     'C': [9, 10, 11, 12]
  6. }
  7. df = pd.DataFrame(data)
  8. print(df.mean())#计算均值
  9. print(df.median())#计算中位数
  10. print(df.mode())#计算众数
  11. print(df.var())#计算方差
  12. print(df.std())#计算标准差
  13. print(df.sum())#计算总和
  14. print(df.min())#计算最小值
  15. print(df.max())#计算最大值
  16. print(df.count())#计算非空值的个数
  17. print(df.prod())#计算乘积
  18. print(df.abs())#计算绝对值
复制代码
  1. A     2.5
  2. B     6.5
  3. C    10.5
  4. dtype: float64
  5. A     2.5
  6. B     6.5
  7. C    10.5
  8. dtype: float64
  9.    A  B   C
  10. 0  1  5   9
  11. 1  2  6  10
  12. 2  3  7  11
  13. 3  4  8  12
  14. A    1.666667
  15. B    1.666667
  16. C    1.666667
  17. dtype: float64
  18. A    1.290994
  19. B    1.290994
  20. C    1.290994
  21. dtype: float64
  22. A    10
  23. B    26
  24. C    42
  25. dtype: int64
  26. A    1
  27. B    5
  28. C    9
  29. dtype: int64
  30. A     4
  31. B     8
  32. C    12
  33. dtype: int64
  34. A    4
  35. B    4
  36. C    4
  37. dtype: int64
  38. A       24
  39. B     1680
  40. C    11880
  41. dtype: int64
  42.    A  B   C
  43. 0  1  5   9
  44. 1  2  6  10
  45. 2  3  7  11
  46. 3  4  8  12
复制代码
6.2重置索引

重置索引(reindex)可以更改原 DataFrame 的行标签或列标签,并使更改后的行、列标签与 DataFrame 中的数据逐一匹配。通过重置索引操作,您可以完成对现有数据的重新排序。如果重置的索引标签在原 DataFrame 中不存在,那么该标签对应的元素值将全部添补为 NaN。
reindex
reindex() 方法用于重新索引 DataFrame 或 Series 对象。重新索引意味着根据新的索引标签重新排列数据,并添补缺失值。如果重置的索引标签在原 DataFrame 中不存在,那么该标签对应的元素值将全部添补为 NaN。


  • 语法
  1. DataFrame.reindex(labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=np.nan, limit=None, tolerance=None)
复制代码
参数

  • labels

    • 类型:数组或列表,默认为 None。
    • 形貌:新的索引标签。

  • index

    • 类型:数组或列表,默认为 None。
    • 形貌:新的行索引标签。

  • columns

    • 类型:数组或列表,默认为 None。
    • 形貌:新的列索引标签。

  • axis

    • 类型:整数或字符串,默认为 None。
    • 形貌:指定重新索引的轴。0 或 ‘index’ 表示行,1 或 ‘columns’ 表示列。

  • method

    • 类型:字符串,默认为 None。
    • 形貌:用于添补缺失值的方法。可选值包罗 ‘ffill’(前向添补)、‘bfill’(后向添补)等。

  • copy:

    • 类型:布尔值,默认为 True。
    • 形貌:是否返回新的 DataFrame 或 Series。

  • level:

    • 类型:整数或级别名称,默认为 None。
    • 形貌:用于多级索引(MultiIndex),指定要重新索引的级别。

  • fill_value

    • 类型:标量,默认为 np.nan。
    • 形貌:用于添补缺失值的值。

  • limit:

    • 类型:整数,默认为 None。
    • 形貌:指定连续添补的最大数目。

  • tolerance:

    • 类型:标量或字典,默认为 None。
    • 形貌:指定重新索引时的容差。



  • 重置索引行
  1. data = {
  2.     'A': [1, 2, 3, 4],
  3.     'B': [5, 6, 7, 8],
  4.     'C': [9, 10, 11, 12]
  5. }
  6. df = pd.DataFrame(data,index=['a','b','c','d'])
  7. #重置索引行
  8. new_index = ['a','b','c','d','e']
  9. print(df.reindex(new_index))#重置索引
复制代码
  1.   A    B     C
  2. a  1.0  5.0   9.0
  3. b  2.0  6.0  10.0
  4. c  3.0  7.0  11.0
  5. d  4.0  8.0  12.0
  6. e  NaN  NaN   NaN
复制代码


  • 重置索引列
  1. data = {
  2.     'A': [1, 2, 3, 4],
  3.     'B': [5, 6, 7, 8],
  4.     'C': [9, 10, 11, 12]
  5. }
  6. df = pd.DataFrame(data,index=['a','b','c','d'])
  7. #重置索引列
  8. new_columns = ['A','B','C','D']
  9. print(df.reindex(columns=new_columns))
复制代码
  1.   A  B   C   D
  2. a  1  5   9 NaN
  3. b  2  6  10 NaN
  4. c  3  7  11 NaN
  5. d  4  8  12 NaN
复制代码


  • 重置索引行,并使用向前添补
  1. data = {
  2.     'A': [1, 2, 3, 4],
  3.     'B': [5, 6, 7, 8],
  4.     'C': [9, 10, 11, 12]
  5. }
  6. df = pd.DataFrame(data,index=['a','b','c','d'])
  7. #重置索引行,并使用向前填充
  8. new_index = ['a','b','c','d','e']
  9. print(df.reindex(new_index,method='ffill'))
复制代码
  1. A  B   C
  2. a  1  5   9
  3. b  2  6  10
  4. c  3  7  11
  5. d  4  8  12
  6. e  4  8  12
复制代码


  • 重置索引行,并使用指定值添补
  1. data = {
  2.     'A': [1, 2, 3, 4],
  3.     'B': [5, 6, 7, 8],
  4.     'C': [9, 10, 11, 12]
  5. }
  6. df = pd.DataFrame(data,index=['a','b','c','d'])
  7. #重置索引行,并使用指定值填充
  8. new_index = ['a','b','c','d','e']
  9. print(df.reindex(new_index,method='ffill',fill_value=0))
复制代码
  1. A  B   C
  2. a  1  5   9
  3. b  2  6  10
  4. c  3  7  11
  5. d  4  8  12
  6. e  4  8  12
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

一给

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表