Series数组创建
- 导入pandas包,pip install pandas(没有下载的小伙伴吗直接在cmd里面运行这句话吧)
1、直接用series
创建
a = pd.Series([1, 2, 3, 4, 5])
print(a)
输出结果:
0 1
1 2
2 3
3 4
4 5
dtype: int64
2、同时创建标签index
值
b = pd.Series([1,2,3,4,5], index = list("abcde"))
print(b)
输出结果:
a 1
b 2
c 3
d 4
e 5
dtype: int64
3、通过字典进行创建
c = {"name": "梨花", "年龄":"18岁", "性别":"🚹"}
d = pd.Series(c)
print(d)
输出结果:
name 梨花
年龄 18岁
性别 🚹
dtype: object
4、对数组的数据结构进行修改
print(a.astype(float))
输出结果;
0 1.0
1 2.0
2 3.0
3 4.0
4 5.0
dtype: float64
索引:
1、通过index
进行索引:
print(d["name"])
print(d[2])
2、通过index
进行一串索引(以0为起点)
e = list(d.index)[:2]
print(e)
输出:
['name', '年龄']
3、通过[[]]
进行索引
e1 = d[["name","性别"]]
print(e1)
输出结果:
name 梨花
性别 🚹
4、通过values
进行索引(索引1就是第一个数字(以1为起点))
e2 = d.values
print(e2)
输出结果:
['梨花' '18岁' '🚹']
5、通过values
进行索引多个量
e3 = list(d.values)[:1]
print(e3)
print(list(d.values)[:2])