程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

pandas. Series concept

編輯:Python

pandas.Series

  • install pandas library
  • Basic concepts
  • Series establish
    • Dictionary creation
    • Array creation
    • Scalar creation
  • name: Name attribute

install pandas library

pip install pandas

Basic concepts

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 Compared with ndarray, Is a built-in index index Array of → One dimensional array + Corresponding index , Just look at series When , It's just one. ndarray
  • series and ndarray More similar , The index slicing functions do not differ much
  • series Compared with dictionaries ,series It's more like a sequential dictionary ( There is no order in the dictionary itself ), Its indexing principle is similar to that of a dictionary ( One use key, One use index)

Series establish

Dictionary creation

# 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)

Array creation

# 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)

Scalar creation

# 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 

name: Name attribute

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)

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved