import pandas as pd
import numpy as np
import names '''
写在前面的话:
1、series与array类型的不同之处为series有索引,而另一个没有;series中的数据必须是一维的,而array类型不一定
2、可以把series看成一个定长的有序字典,可以通过shape,index,values等得到series的属性
'''
# 1、series的创建
'''
(1)由列表或numpy数组创建
默认索引为0到N-1的整数型索引,如s1;
可以通过设置index参数指定索引,如s2;
通过这种方式创建的series,不是array的副本,即对series操作的同时也改变了原先的array数组,如s3
(2)由字典创建
字典的键名为索引,键值为值,如s4;
'''
n1 = np.array([1, 4, 5, 67, 7, 43, ])
s1 = pd.Series(n1)
# print(s1)
'''
0 1
1 4
2 5
3 67
4 7
5 43
dtype: int32
'''
s2 = pd.Series(n1, index=['a', 'b', 'c', 'd', 'e', 'f'])
# print(s2)
'''
a 1
b 4
c 5
d 67
e 7
f 43
dtype: int32
'''
# print(n1)
'''
[ 1 4 5 67 7 43]
'''
s1[2] = 100
s3 = s1
# print(s3)
'''
0 1
1 4
2 100
3 67
4 7
5 43
dtype: int32
'''
# print(n1)
'''
[ 1 4 100 67 7 43]
'''
dict1 = {}
for i in range(10, 15):
# names.get_last_name(),随机生成英文名字
dict1[names.get_last_name()] = i
s4 = pd.Series(dict1)
# print(s4)
'''
Poole 10
Allen 11
Davis 12
Roland 13
Brehm 14
dtype: int64
'''
# 2、series的索引
'''
(1)通过index取值,可以通过下标获取,也可以通过指定索引获取,如s6,s7
(2)通过.loc[](显示索引)获取,这种方式只能获取显示出来的索引,无法通过下标获取,如s7(推荐)
(3)隐式索引,使用整数作为索引值,使用.icol[],如s9(推荐)
'''
s5 = pd.Series(np.array([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')])
# print(s5)
'''
a 1
b 5
c 9
d 7
e 6
f 4
g 52
h 8
dtype: int32
'''
s6 = s5[2]
# print(s6)
'''
9
'''
s7 = s5['c']
# print(s7)
'''
c 9
dtype: int32
'''
s8 = s5.loc['c']
# print(s8)
'''
c 9
dtype: int32
'''
s9 = s5.iloc[2]
# print(s9)
'''
9
'''
# 3、series的切片
'''
1、series的切片和列表的用法类似,不同之处在于建议使用.loc[:]和.iloc[:],如s10和s11。当然直接使用[:]也可以。
2、当遇到特别长的series,我们支取出前5条或后5条数据时可以直接使用.head()或.tail()
'''
s5 = pd.Series(np.array([1, 5, 9, 7, 6, 4, 52, 8]), index=[list('abcdefgh')])
# print(s5)
'''
a 1
b 5
c 9
d 7
e 6
f 4
g 52
h 8
dtype: int32
'''
s10 = s5.loc['b':'g']
# print(s10)
'''
b 5
c 9
d 7
e 6
f 4
g 52
dtype: int32
'''
s11 = s5.iloc[1:7]
# print(s11)
'''
b 5
c 9
d 7
e 6
f 4
g 52
dtype: int32
'''
# 4、关于NaN
'''
(1)NaN是代表空值, 但不等于None。两者的数据类型不一样,None的类型为<class 'NoneType'>,而NaN的类型为<class 'float'>;
(2)可以使用pd.isnull(),pd.notnull(),或自带isnull(),notnull()函数检测缺失数据
'''
# print(type(None),type(np.nan))
'''
<class 'NoneType'> <class 'float'>
'''
s12 = pd.Series([1,2,None,np.nan],index=list('烽火雷电'))
# print(s12)
'''
烽 1.0
火 2.0
雷 NaN
电 NaN
dtype: float64
'''
# print(pd.isnull(s12))
'''
烽 False
火 False
雷 True
电 True
dtype: bool
'''
# print(pd.notnull(s12))
'''
烽 True
火 True
雷 False
电 False
dtype: bool
'''
# print(s12.notnull())
'''
烽 True
火 True
雷 False
电 False
dtype: bool
'''
# print(s12.isnull())
'''
烽 False
火 False
雷 True
电 True
dtype: bool
'''
# 取出series中不为空的值
# print(s12[s12.notnull()])
'''
烽 1.0
火 2.0
dtype: float64
'''
# series的name属性
''' '''
s12.name = '风水'
# print(s12)
'''
烽 1.0
火 2.0
雷 NaN
电 NaN
Name: 风水, dtype: float64
'''

pandas中的series数据类型的更多相关文章

  1. pandas中数据结构-Series

    pandas中数据结构-Series pandas简介 Pandas是一个开源的,BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Python与Pan ...

  2. pandas中的Series

    我们使用pandas经常会用到其下面的一个类:Series,那么这个类都有哪些方法呢?另外Series和DataFrame都继承了NDFrame这个类,df.to_sql()这个方法其实就是NDFra ...

  3. numpy中的ndarray与pandas中的series、dataframe的转换

    一个ndarray是一个多维同类数据容器.每一个数组有一个dtype属性,用来描述数组的数据类型. Series是一种一维数组型对象,包含了一个值序列,并且包含了数据标签----索引(index). ...

  4. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  5. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  6. 6.2Python数据处理篇之pandas学习系列(二)Series数据类型

    目录 目录 (一)Series的组成 (二)Series的创建 1.从标量中创建Series数据 2.从列表中创建Series数据 3.从字典中创建Series数据 4.从ndarry中创建Serie ...

  7. Pandas中Series和DataFrame的索引

    在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引.比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字. ...

  8. pandas中的分组技术

    目录 1  分组操作 1.1  按照列进行分组 1.2  按照字典进行分组 1.3  根据函数进行分组 1.4  按照list组合 1.5  按照索引级别进行分组 2  分组运算 2.1  agg 2 ...

  9. pandas中的空值处理

    1.空值 1.1 有两种丢失数据: None: Python自带的数据类型 不能参与到任何计算中 np.nan: float类型 能参与计算,但结果总是nan # None+2 # 报错 # np.n ...

随机推荐

  1. HTML 【表单】

    表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签. 表单域.表单按钮. 表单标签 < form  action = " "  method = &q ...

  2. vue + element ui 实现实现动态渲染表格

    前言:之前需要做一个页面,能够通过表名动态渲染出不同的表格,这里记录一下.转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9786326.html 网站地址:我的 ...

  3. php命令行生成与读取配置文件

    接着之前的文章:php根据命令行参数生成配置文件 ghostinit.php <?php class ghostinit{ static $v = 'ghost version is 1.1'; ...

  4. bzoj1758Wc10重建计划——solution

    1758: [Wc2010]重建计划 Time Limit: 40 Sec  Memory Limit: 162 MBSubmit: 4707  Solved: 1200[Submit][Status ...

  5. idea 快捷键总结

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  6. css3 content 特殊字符和符号

    基本形状 ▲ 9650 25B2 ► 9658 25BA ► 9658 25BA ▼ 9660 25BC◄ 9668 25C4 ❤ 10084 2764 ✈ 9992 2708 ★ 9733 2605 ...

  7. 【CLR Via C#】16 数组

    所有的数组都隐式的从System.Array抽象类派生,后者又派生自System.Object 数组是引用类型,所以会在托管堆上分配内存,数组对象占据的内存块包含数组的元素,一个类型对象指针.一个同步 ...

  8. Python笔记(三):构建发布模块

      (一)     准备工作 1.   新建一个模块(名称自定义),存放要发布的模块代码. 2.   新建一个setup.py的模块(存放模块的元数据,描述相关信息). 3.   新建一个文件夹(名称 ...

  9. 从ibd文件获取表空间id

    xtrabackup恢复过程中出现如下错误 InnoDB: Doing recovery: scanned up to log sequence number ( %) InnoDB: Doing r ...

  10. c# 通过GroupBy 进行分组

    有时候我们需要数据根据一些字段进行分组,这时候用orderBy很方便.不多说了.直接上代码: class Ma { public int number { get; set; } public str ...