一、Series
1、通过list构建:
pd.Series(range(10, 20))
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
dtype: int64
2、通过ndarray
pd.Series(np.random.rand(5))
0 0.969940
1 0.023138
2 0.872141
3 0.093144
4 0.857817
dtype: float64
3、 构建时指定索引
pd.Series(np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'])
a 0.736392
b 0.894126
c 0.139299
d 0.630322
e 0.920788
dtype: float64
4、数据预览
# 构建时指定索引
ser_obj = pd.Series(np.random.rand(100))
ser_obj.head()
#查看前五列数据
ser_obj,tail()
#查看后五列数据
#通过索引获取数据
pd.Series(np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'])
# 通过索引名(字符串)获取数据
ser_obj2['b']
ser_obj2.loc['b']
# 通过in判断数据是否存在
'a' in ser_obj2
# 通过索引位置(整型)获取数据
ser_obj2[0]
ser_obj2.iloc[0]
5、处理缺失数据
# 字符串缺失
countries = ['中国', '美国', '澳大利亚', None]
pd.Series(countries)
'''
output
0 中国
1 美国
2 澳大利亚
3 None
dtype: object
'''
# 数值缺失
numbers = [4, 5, 6, None]
pd.Series(numbers)
'''
output
0 4.0
1 5.0
2 6.0
3 NaN
dtype: float64
'''