pandas之Series基本使用

import string
import pandas as pd
t=pd.Series([1,2,31,12,3,4]) #创建一个简单的series
type(t)
pandas.core.series.Series
t
0     1
1     2
2    31
3    12
4     3
5     4
dtype: int64
t2=pd.Series([1,23,2,2,1],index=list("abcde")) #list对应键值
t2
a     1
b    23
c     2
d     2
e     1
dtype: int64
temp_dict={"a":"a1","b":"b2","c":"c3"}
t3=pd.Series(temp_dict) #字典创建series,键对应index,值对应值
t3
a    a1
b    b2
c    c3
dtype: object
t3.dtype
dtype('O')
t2.dtype
dtype('int64')
t2.astype(float) #修改dtype,和numpy一样的方法
a     1.0
b    23.0
c     2.0
d     2.0
e     1.0
dtype: float64
print(t3["a"])
print(t3["b"]) #取值和字典一样
print(t3[1]) #或者用index索引取值
print("*"*5)
print(t3[:1]) #取连续的行 
print("*"*5)
print(t3[[1,2]]) #取不连续的行
print("*"*5)
print(t3[["c","b"]]) #取指定的行
a1
b2
b2
*****
a    a1
dtype: object
*****
b    b2
c    c3
dtype: object
*****
c    c3
b    b2
dtype: object
t[t>4] #通过比较大小index取值
2    31
3    12
dtype: int64
print(t3.index)
print(type(t3.index)) #可迭代类型
for i in t3.index:
    print(i)
print("*"*5)
print(list(t3.index)[:2]) #可以跟列表一样操作
print("*"*5)
print(type(t3.values)) #值是numpy.ndarray类型,object,也是可以切片遍历索引
Index(['a', 'b', 'c'], dtype='object')
<class 'pandas.core.indexes.base.Index'>
a
b
c
*****
['a', 'b']
*****
<class 'numpy.ndarray'>



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值