Try   HackMD

25. Python Pandas 資料分析 - Series 單維度資料 By 彭彭

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

建立series

  • 載入Pandas模組
    import pandas as pd
  • 以列表資料為底,建立Series
    pd.Series(資料列表)

資料索引

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

資料的編號(最左邊) 在程式中稱為索引 Index

內建索引

  • 載入pandas模組
    import pandas as pd
  • 以列表資料為底,建立Series
    pd. Series(資料列表)

自訂索引

  • 載入pandas模組
    import pandas as pd
  • 以列表資料為底,建立Series
    pd. Series(資料列表, index=索引列表)

觀察資料

資料型態

import pandas as pd
data=pd. Series(資料列表)
印出dtype屬性
print(data.dtype)

資料型態:有多少筆資料

import pandas as pd
data=pd. Series (資料列表)
印出sizee屬性
print(data.size)

資料索引

import pandas as pd
data=pd. Series(資料列表, index=索引列表)
印出index屬性
print(data.index)


取得資料

根據順序取值

import pandas as pd
data=pd.Series(資料列表)
取得資料data[順序]
print(data[1])
順序從0開始 所以1代表是第二個資料

根據索引取值

import pandas as pd
data=pd.Series(資料列表, index=索引列表)
取得資料data[索引]
print(data[索引])


數字運算

數學、統計相關

import pandas as pd
data=pd.Series([3,10,20,5,-12])
(各種數學、統計運算)
print(data.sum(,data.max(),data.prod())
prod是算出乘法的總和,全部相乘
print(data.mean(), data.median(),data.std())
mean是算出平均數
median是中位數
std是標準差
print(data.nlargest(3),data.nsmallest(2))
nlargest取(前3大)的數字
nsmallest 取最小的(2)個數字

字串運算

字串操作相關

import pandas as pd
data=pd.Series(["您好","Python","Pandas"])
各種字串操作,都定義在str底下
print(data.str.lower(),data.str.upper(),data.str.len())
(lower 把所有字串變小寫upper把所有字串變大寫len得到每一個字串長度)
print(data.str.cat(sep=","),data.str.contains("P"))
sep把字串串再一起,用(逗號)
contains 判斷字串中是否有(大寫P)
print(data.str.replace("您好","Hello"))
replace字串取代,把您好取代成Hello

End