Serise的用法,就像表中的列一樣
他可以從list建立一個簡單的pandas Serise
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a)
print(myvar)
他的輸出結果會將a 設為第一列
輸出
(標籤) | (Series) |
---|---|
0 | 1 |
1 | 7 |
2 | 2 |
dtype: int64
如果要回傳某一個標籤print(myvar[0]) 輸出 1
如果你想改標籤列的名稱,可以在pd.Series(a, index = ["x", "y", "z"])
加入index = ["名稱1","名稱2","名稱3"]
import pandas as pd
a = [1, 7, 2]
myvar = pd.Series(a, index = ["a", "b", "c"])
print(myvar)
(標籤) | (Series) |
---|---|
a | 1 |
b | 7 |
c | 2 |
dtype: int64
可以回傳某一個["b"]試試看print(myvar["b"]) 輸出 7
上面的a換成字典,假設a = {"a":10, "b":20, "c":30}
就可以直接生成 ,如果整個a,只想要b,c,其他不想要,我麼可以用index
import pandas as pd
a = {"a":10, "b":20, "c":30}
myvar = pd.Series(a, index = ["b", "c"])
print(myvar)
(標籤) | (Series) |
---|---|
b | 20 |
c | 30 |
dtype: int64
下一篇我們聊Pandas 中最多資料集的多為表, DataFrame!!