s = pd.Series([1,3,5,np.nan,6,8])
结果: 0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
s[2:5]
结果: 2 5.0
3 NaN
4 6.0
索引赋值
s = pd.Series([1,3,5,np.nan,6,8])
------------索引赋值1
s.index.name = '索引'
结果: 索引
0 1.0
1 3.0
2 5.0
3 NaN
4 6.0
5 8.0
dtype: float64
--------------索引赋值2
s.index=list('abcdef')
结果:
a 1.0
b 3.0
c 5.0
d NaN
e 6.0
f 8.0
dtype: float64
----------取值
s['a':'c']
结果:
a 1.0
b 3.0
c 5.0
dtype: float64
读取数据及数据操作
行操作
读
iloc
df.iloc[0] #读取第0行
df.iloc[0:5] #读取0-4行
loc
df.loc[0:5] #读取0-5行
添加
dit = {'名字':'复仇者联盟3','投票人数':123,'类型':'剧情','产地':'美国','上映时间':'2018-05-4 00:00:00',...}
s = pd.Series(dit)
s.name = 38738
df = df.append(s) #df覆盖原来的数据对象
#结果: 在第38738行添加了数据