Series的创建
##数据分析汇总学习
https://blog.csdn.net/weixin_39778570/article/details/81157884
# 使用列表创建
>>> import numpy as np
>>> import pandas as pd
>>> s1 = pd.Series([1,2,3,4])
>>> s1
0 1
1 2
2 3
3 4
dtype: int64
# 查看s1的值和索引
>>> s1.values
array([1, 2, 3, 4], dtype=int64)
>>> s1.index
RangeIndex(start=0, stop=4, step=1) # 默认索引
# 使用数组创建
>>> s2 = pd.Series(np.arange(10))
>>> s2
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
dtype: int32
# 使用字典创建
>>> s3 = pd.Series({'1':1, '2':2, '3':3})
>>> s3
1 1
2 2
3 3
dtype: int64
>>> s3.values
array([1, 2, 3], dtype=int64)
>>> s3.index
Index(['1', '2', '3'], dtype='object')
Series的访问
>>> s4 = pd.Series([1,2,3,4], index = ['a','b','c','d'])
>>> s4
a 1
b 2
c 3
d 4
dtype: int64
>>> s4.values
array([1, 2, 3, 4], dtype=int64)
>>> s4.index
Index(['a', 'b', 'c', 'd'], dtype='object')
>>> s4['a'] # 访问索引为a的值
1
>>> s4[s4>2] #访问s4中值大于2的Series
c 3
d 4
dtype: int64
# Series与字典的转换
>>> s4.to_dict() # s4转换为字典
{'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> s5 = pd.Series(s4.to_dict()) # 字典转换为Series
>>> s5
a 1
b 2
c 3
d 4
dtype: int64
# e索引无值补充为NaN
>>> index_1 = ['a','b','c','d','e']
>>> s6 = pd.Series(s5, index = index_1)
>>> s6
a 1.0
b 2.0
c 3.0
d 4.0
e NaN # s5此处无值
dtype: float64
# NaN判断
>>> pd.isnull(s6)
a False
b False
c False
d False
e True
dtype: bool
>>> pd.notnull(s6)
a True
b True
c True
d True
e False
dtype: bool
# 命名修改
>>> s6.name = 'demo' # s6的名字修改
>>> s6
a 1.0
b 2.0
c 3.0
d 4.0
e NaN
Name: demo, dtype: float64
>>> s6.index.name = 'demo_index' # s6的索引的名字的修改
>>> s6.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object', name='demo_index')
官网:http://pandas.pydata.org/pandas-docs/version/0.14.1/