Pandas入门系列(一)-- Series

Series的创建

##数据分析汇总学习

https://blog.csdn.net/weixin_39778570/article/details/81157884

# 使用列表创建

1 >>> import numpy as np
2 >>> import pandas as pd
3 >>> s1 = pd.Series([1,2,3,4])
4 >>> s1
5 0    1
6 1    2
7 2    3
8 3    4
9 dtype: int64
1 # 查看s1的值和索引
2 >>> s1.values
3 array([1, 2, 3, 4], dtype=int64)
4 >>> s1.index
5 RangeIndex(start=0, stop=4, step=1) # 默认索引

# 使用数组创建

 1 >>> s2 = pd.Series(np.arange(10))
 2 >>> s2
 3 0    0
 4 1    1
 5 2    2
 6 3    3
 7 4    4
 8 5    5
 9 6    6
10 7    7
11 8    8
12 9    9
13 dtype: int32

# 使用字典创建

 1 >>> s3 = pd.Series({'1':1, '2':2, '3':3})
 2 >>> s3
 3 1    1
 4 2    2
 5 3    3
 6 dtype: int64
 7 >>> s3.values
 8 array([1, 2, 3], dtype=int64)
 9 >>> s3.index
10 Index(['1', '2', '3'], dtype='object')

Series的访问

 1 >>> s4 =  pd.Series([1,2,3,4], index = ['a','b','c','d'])
 2 >>> s4
 3 a    1
 4 b    2
 5 c    3
 6 d    4
 7 dtype: int64
 8 >>> s4.values
 9 array([1, 2, 3, 4], dtype=int64)
10 >>> s4.index
11 Index(['a', 'b', 'c', 'd'], dtype='object')
12 >>> s4['a'] # 访问索引为a的值
13 1
14 >>> s4[s4>2] #访问s4中值大于2的Series
15 c    3
16 d    4
17 dtype: int64

# Series与字典的转换

 1 >>> s4.to_dict()  # s4转换为字典
 2 {'a': 1, 'b': 2, 'c': 3, 'd': 4}
 3  
 4  
 5 >>> s5 = pd.Series(s4.to_dict())  # 字典转换为Series
 6 >>> s5
 7 a    1
 8 b    2
 9 c    3
10 d    4
11 dtype: int64

# e索引无值补充为NaN

1 >>> index_1 = ['a','b','c','d','e']
2 >>> s6 = pd.Series(s5, index = index_1)
3 >>> s6
4 a    1.0
5 b    2.0
6 c    3.0
7 d    4.0
8 e    NaN  # s5此处无值
9 dtype: float64

# NaN判断

 1 >>> pd.isnull(s6)
 2 a    False
 3 b    False
 4 c    False
 5 d    False
 6 e     True
 7 dtype: bool
 8 >>> pd.notnull(s6)
 9 a     True
10 b     True
11 c     True
12 d     True
13 e    False
14 dtype: bool

# 命名修改

 1 >>> s6.name = 'demo'   # s6的名字修改
 2 >>> s6
 3 a    1.0
 4 b    2.0
 5 c    3.0
 6 d    4.0
 7 e    NaN
 8 Name: demo, dtype: float64
 9  
10 >>> s6.index.name = 'demo_index'  # s6的索引的名字的修改
11 >>> s6.index
12 Index(['a', 'b', 'c', 'd', 'e'], dtype='object', name='demo_index')

官网:http://pandas.pydata.org/pandas-docs/version/0.14.1/

如果还有问题未能得到解决,搜索887934385交流群,进入后下载资料工具安装包等。最后,感谢观看!

原文地址:https://www.cnblogs.com/pypypy/p/11930739.html