pip install pandas
Series Is a one-dimensional array with labels , You can save any data type , Axis labels are collectively referred to as index
# Import numpy、pandas modular
import numpy
import pandas
a = numpy.random.rand(5)
s = pandas.Series(a)
print(s)
print(type(s)) # View data type
# .index: see series Indexes , The type is rangeindex
print(s.index,type(s.index))
# .values: see series value , The type is ndarray
print(s.values,type(s.values))
# Series Create method one : Created by dictionary , Dictionary key Namely index,values Namely values
import numpy
import pandas
dic = {
"a":1,"b":2,"c":3}
s = pandas.Series(dic)
print(s)
# Series Create method two : Created by array ( One dimensional array )
import numpy
import pandas
ar = numpy.random.randn(5)
s = pandas.Series(ar)
print(ar)
print(s) # Default index It's from 0 Start , In steps of 1 The number of
s = pandas.Series(ar,index = ["a","b","c","d","e"],dtype = numpy.object)
# index Parameters : Set up index, Keep the same length
# dtype Parameters : Set the value type
print(s)
# Series Create method three : Created by scalar
import numpy
import pandas
s = pandas.Series(10,index = range(0,4))
print(s)
# If data Is a scalar value , An index must be provided . The value repeats , To match the length of the index
import numpy
import pandas
# name by Series A parameter of , Create an array name
s = pandas.Series(numpy.random.rand(5),name = "test")
print(s)
# .name Method : The name of the output array , The output format is str, If the output name is not defined , Output is None
s2 = s.rename("demo")
# .rename() Rename the name of an array , And new points to an array , The original array remains the same
print(s)
print(s2)