0
点赞
收藏
分享

微信扫一扫

pandas的引入及Series的基础操作

天蓝Sea 2022-05-02 阅读 87
作用

相对于numpy,pandas更进一步能帮助处理数值型数据之外的其他类型数据(比如时间序列、字符串等);

常用数据类型
  • Series:一维,带标签数组
  • DataFrame:二维,Series容器

Series

创建Series数组
  • 语法结构:
    pd.series(数据序列, index=索引序列)
    
    • 举例:pd.Series(np.arange(11), index=list("asdfghjkkll"))
      输出时一一对应;
    • 索引序列:
      • 大写字母构成序列:
        import string
        string.ascii_uppercase[i] for i in range(10)
        
        • 生成字符串格式大写字母序列;
        • 最多26位;
  • 举例:
    import pandas as pd
    import numpy as np
    
    s = pd.Series(np.arange(11))
    print(type(s))
    print(s)
    
    • 其中数据类型为:pandas.core.series.Series;
    • 对数组进行打印结果为:
      0      0
      1      1
      2      2
      3      3
      4      4
      5      5
      6      6
      7      7
      8      8
      9      9
      10    10
      dtype: int32
      
  • pandas中会自动根据数据类型更改series的dtype类型;
  • 当指定索引无对应值时,为nan;
  • 通过字典创建时,键值对分别对应series的索引和值;
    temp_dict = {"name":"Lee", "age":24}
    s = pd.Series(temp_dict)
    print(s["name"])
    print(s[0])
    
    输出均为:Lee
Series的索引和切片操作
  • 通过位置和索引取值:
    temp_dict = {"name":"Lee", "age":24}
    s = pd.Series(temp_dict)
    print(s["name"])
    print(s[0])
    
    输出均为:Lee
  • 取前n行:
    print(s[:n])
  • 取第m、n行:
    print(s[m, n])
  • 按步长取行:
    print(s[start: stop: step])
    -在值为数值时可按大小关系取行:
    print(s[s>n])
举报

相关推荐

0 条评论