# -*- coding: utf-8 -*-
import pandas as pd
s =pd.Series([“a”,”b”,”c”,”d”])
#s為pandas物件
#<class ‘pandas.core.series.Series’>
print(“Series:\n”,s)
print(“s.index:\n”,s.index,type(s.index)) #index結尾沒有s
print(“s.values:\n”,s.values,type(s.values)) #values結尾有s
#像字典的dic.keys() ; dic.values()
print(“list(index):\n”,list(s.index))
print(“list(values):\n”,list(s.values))
print(“取值:”,s[2])
print(“取值:\n”,s[[2,3]])
#有兩個[]
print(“dict(s):\n”,dict(s))
#pandas物件有index跟values,可以轉為dict
dic={“k1″:”v1″,”k2″:”v2”}
print(“字典轉為pandas物件\n”,pd.Series(dic))
#相對地,字典也可以轉為pandas物件
輸出結果:
pandas物件取值:
import pandas as pd
s =pd.Series([“a”,”b”,”c”,”d”])
print(“pandas物件:\n”,s)
print(“取值:”,s[1]) #b
print(“取值:\n”,s[1:999]) #b,c,d
print(“取值:\n”,s[1:]) #b,c,d
print(“取值:\n”,s[1:-1]) #b,c(沒有d)
print(“取值:\n”,s[1::2]) #b,d
輸出:
推薦hahow線上學習python: https://igrape.net/30afN
近期留言