import pandas as pd
import numpy as np
from pandas import Series,DataFrame
文章目录
1,Series
- pandas.Series() 返回一个有 index 和 values 属性的数对象
s=pd.Series([4,5,-7,3])
s
0 4
1 5
2 -7
3 3
dtype: int64
s.index
RangeIndex(start=0, stop=4, step=1)
s.values
array([ 4, 5, -7, 3], dtype=int64)
自定义索引
s1=Series([4,7,6,5,1],index=['a','b','c','d','e'],dtype=float)
s1
a 4.0
b 7.0
c 6.0
d 5.0
e 1.0
dtype: float64
s1.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
读取 Series
- 1,使用 0-n 的索引
- 2,自定义的索引
- #数字索引读取
s1[0]
4.0
- #自定义索引读取
s1['a']
4.0
- 如果自定义索引是纯数字,就不能用 0 -n 的索引读取了,只能用自定义
s2=Series([4,7,6,5], index=[3,5,7,9],dtype=np.float64)
s2
3 4.0
5 7.0
7 6.0
9 5.0
dtype: float64
s2[3]
4.0
Sereis.loc[‘自定义的索引’] || Sereis.iloc[0-n的数字]
s2.loc[7]
6.0
s2.iloc[2]
6.0
xxx.loc[‘yy’]=new 修改和增加
s1
a 4.0
b 7.0
c 6.0
d 5.0
e 1.0
dtype: float64
s1.loc['e']=1
s1
a 4.0
b 7.0
c 6.0
d 5.0
e 1.0
dtype: float64
</