Pandas 数据结构-Series

作为自己学习的笔记,加油!

 import pandas as pd

     Series是一种一维的数组型对象。包含两个属性Series.values 和Series.index

1.1创建Series

    from pandas import Series
    obj=Series([4,5,6,7])
     obj

  0    4
  1    5
  2    6
  3    7
  dtype: int64

   1、1、1设置索引

  默认索引是0开始,也可以设置索引值。

   from pandas import Series
   obj=Series([4,5,6,7],index=['a','b','c','d'])
   obj

 a    4
 b    5
 c    6
 d    7
 dtype: int64

  1、2 获得series对象的值

   obj.values

1、3 获得series 对象的索引

obj.index

1、4 通过索引获得对应值

obj['a']

1、5 过滤 选择大于4的值

obj[obj>4]

1、6 计算

obj*2 结果[8,10,12,14]

1、7 判断5是否在obj中  

5 in obj

for b in obj:####输出的是  a b c d

      print(b)

for b in obj.index:

for b in obj.values ==for b in obj

1、8  字典转化为Series

data={'q':3,'e':1,'t':-2,'r':5}

d=Series(data)产生的结果可能随机
d=Series(data,index=['q','t','e','r'])  按照想要的顺序输出

d=Series(data,index=['w','t','e','r'])

因为不存在'w'所以该值输出为NaN

w    NaN
t   -2.0
e    1.0
r    5.0

1、9 判断是否为空

d.isnull()

w     True
t    False
e    False
r    False

d.notnull()

或则pd.isnull(d)

pd.inotnull(d)

2、0 series 本身和索引都有name属性

d.name='hello'

d.index.name='nihao '

输出:

nihao 
w    NaN
t   -2.0
e    1.0
r    5.0
Name: hello, dtype: float64

   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章