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

   

 

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