博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Pandas -- Series
阅读量:5973 次
发布时间:2019-06-19

本文共 3631 字,大约阅读时间需要 12 分钟。

pandas.Series

class pandas.Series(data=Noneindex=Nonedtype=Nonename=Nonecopy=Falsefastpath=False)

One-dimensional ndarray with axis labels (including time series).

Labels need not be unique but must be any hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Statistical methods from ndarray have been overridden to automatically exclude missing data (currently represented as NaN)

Operations between Series (+, -, /, , *) align values based on their associated index values– they need not be the same length. The result index will be the sorted union of the two indexes.

Parameters :

data : array-like, dict, or scalar value

Contains data stored in Series

index : array-like or Index (1d)

Values must be unique and hashable, same length as data. Index object (or other iterable of same length as data) Will default to np.arange(len(data)) if not provided. If both a dict and index sequence are used, the index will override the keys found in the dict.

dtype : numpy.dtype or None

If None, dtype will be inferred

copy : boolean, default False

Copy input data

Series 类似数组,但是它有标签(label) 或者索引(index).

1. 从最简单的series开始看。

from pandas import Series, DataFrameimport pandas as pd  ser1 = Series([1,2,3,4])print(ser1)#0    1#1    2#2    3#3    4#dtype: int64

此时因为没有设置index,所以用默认

2. 加上索引

ser2 = Series(range(4),index=['a','b','c','d'])print(ser2)#a    0#b    1#c    2#d    3#dtype: int64

3. dictionnary 作为输入

dict1 = {
'ohio':35000,'Texas':71000,'Oregon':1600,'Utah':500}ser3 = Series(dict1)#Oregon 1600#Texas 71000#Utah 500#ohio 35000#dtype: int64

key:默认设置为index

dict1 = {
'ohio':35000,'Texas':71000,'Oregon':1600,'Utah':500}ser3 = Series(dict1)#Oregon 1600#Texas 71000#Utah 500#ohio 35000#dtype: int64print(ser3)states = ['California', 'Ohio', 'Oregon', 'Texas']ser4 = Series(dict1,index = states)print(ser4)#California NaN#Ohio NaN#Oregon 1600.0#Texas 71000.0#dtype: float64

用了dictionary时候,也是可以特定的制定index的,当没有map到value的时候,给NaN.

print(pd.isnull(ser4))#California     True#Ohio           True#Oregon        False#Texas         False#dtype: bool

函数isnull判断是否为null

print(pd.isnull(ser4))#California     True#Ohio           True#Oregon        False#Texas         False#dtype: bool

 

函数notnull判断是否为非null

print(pd.notnull(ser4))#California    False#Ohio          False#Oregon         True#Texas          True#dtype: bool

 

4. 访问元素和索引用法

print (ser2['a']) #0#print (ser2['a','c']) errorprint (ser2[['a','c']]) #a    0#c    2#dtype: int64print(ser2.values) #[0 1 2 3]print(ser2.index) #Index(['a', 'b', 'c', 'd'], dtype='object')

5. 运算, pandas的series保留Numpy的数组操作

print(ser2[ser2>2])#d    3#dtype: int64print(ser2*2)#a    0#b    2#c    4#d    6#dtype: int64print(np.exp(ser2))#a     1.000000#b     2.718282#c     7.389056#d    20.085537#dtype: float64

6. series 的自动匹配,这个有点类似sql中的full join,会基于索引键链接,没有的设置为null

print (ser3+ser4)#California         NaN#Ohio               NaN#Oregon          3200.0#Texas         142000.0#Utah               NaN#ohio               NaN#dtype: float64

7. series对象和索引都有一个name属性

ser4.index.name = 'state'ser4.name = 'population count'print(ser4)#state#California        NaN#Ohio              NaN#Oregon         1600.0#Texas         71000.0#Name: population count, dtype: float64

 8.预览数据

print(ser4.head(2))print(ser4.tail(2))#state#California   NaN#Ohio         NaN#Name: population count, dtype: float64#state#Oregon     1600.0#Texas     71000.0#Name: population count, dtype: float64

 

转载于:https://www.cnblogs.com/Jesse-Li/p/8807588.html

你可能感兴趣的文章
物联网对企业的影响
查看>>
问世十年,深度学习有哪些里程碑
查看>>
生活不够精彩?因为你少了这些智能家居产品
查看>>
FAQ系列 | 添加自增列失败
查看>>
密码管理公司 OneLogin 遭入侵,大量账号密码泄露
查看>>
清华计算机系舒继武 CCF-ADL 讲习班上篇:闪存存储系统的软件
查看>>
Facebook免费从用户身上获得好处的日子到头了
查看>>
【转】动态字节码技术跟踪Java程序
查看>>
因需制云,每一抹“红”都不同!
查看>>
泰利特推出五款新的LTE物联网模块
查看>>
OPM泄漏事故报告:矛头直指领导对数据丢失无作为
查看>>
复牌+高层变动:海润光伏还有什么大动作?
查看>>
频繁宕机引发的思考:IDC服务商服务能力亟需提升
查看>>
OA办公系统如何实现费控管理?
查看>>
聂君:企业信息安全建设的思考
查看>>
存储系统市场将面临新的挑战
查看>>
三星占据全球芯片市场11.3% 与英特尔差距缩小
查看>>
抢占10nm市场 联发科将增加Helio X35
查看>>
“野蛮生长”的商业WiFi 退去虚火后该怎么走?
查看>>
想了解双路塔式服务器最新动态吗?
查看>>