pip install pandas
Series 是帶有標簽的一維數組,可以保存任何數據類型,軸標簽統稱為索引
# 導入numpy、pandas模塊
import numpy
import pandas
a = numpy.random.rand(5)
s = pandas.Series(a)
print(s)
print(type(s)) # 查看數據類型
# .index:查看series索引,類型為rangeindex
print(s.index,type(s.index))
# .values:查看series值,類型是ndarray
print(s.values,type(s.values))
# Series 創建方法一:由字典創建,字典的key就是index,values就是values
import numpy
import pandas
dic = {
"a":1,"b":2,"c":3}
s = pandas.Series(dic)
print(s)
# Series 創建方法二:由數組創建(一維數組)
import numpy
import pandas
ar = numpy.random.randn(5)
s = pandas.Series(ar)
print(ar)
print(s) # 默認index是從0開始,步長為1的數字
s = pandas.Series(ar,index = ["a","b","c","d","e"],dtype = numpy.object)
# index參數:設置index,長度保持一致
# dtype參數:設置數值類型
print(s)
# Series 創建方法三:由標量創建
import numpy
import pandas
s = pandas.Series(10,index = range(0,4))
print(s)
# 如果data是標量值,則必須提供索引。該值會重復,來匹配索引的長度
import numpy
import pandas
# name為Series的一個參數,創建一個數組的名稱
s = pandas.Series(numpy.random.rand(5),name = "test")
print(s)
# .name方法:輸出數組的名稱,輸出格式為str,如果沒用定義輸出名稱,輸出為None
s2 = s.rename("demo")
# .rename()重命名一個數組的名稱,並且新指向一個數組,原數組不變
print(s)
print(s2)
A few days ago, I found a gian
{error_code: 54003, error_msg: