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

pandas. Series basic skills

編輯:Python

pandas.Series Basic skills

  • append: add to
  • drop: Delete
  • modify
  • head / tail: View
  • reindex: Re index
  • alignment

append: add to

import numpy
import pandas
# Subscript indices / label index add value 
s1 = pandas.Series(numpy.random.rand(5))
s2 = pandas.Series(numpy.random.rand(5), index = list('abcde'))
s1[5] = 100
s2["f"] = 100
print(s1)
print(s2)
# .append Method to add an array directly 
# .append Method to generate a new array , Do not change the previous array 
s3 = s1.append(s2)
print(s3)

drop: Delete

import numpy
import pandas
s = pandas.Series(numpy.random.rand(5), index = list('abcde'))
print(s)
s1 = s.drop("a")
print(s1)
# inplace: The return value after deleting the element , The default is False
s2 = s.drop(["b","c"],inplace = True)
# If inplace by True, The return value is None
print(s2)

modify

s = pandas.Series(numpy.random.rand(3),index = ['a','b','c'])
# Modify the index directly , Similar list 
s['a'] = 100
s[['b','c']] = 200
print(s)

head / tail: View

import numpy
import pandas
s = pandas.Series(numpy.random.rand(15))
print(s.head(2)) # .head() Look at the header data , View by default 5 strip 
print(s.tail()) # .tail() Check the tail data , View by default 5 strip 

reindex: Re index

import numpy
import pandas
s = pandas.Series(numpy.random.rand(5),index = list("abcde"))
print(s)
# .reindex Will be reordered according to the new index , If the current index does not exist , Then the missing value is introduced 
s2 = s.reindex(list("bcfea")) # .reindex() Also write a list in 
# here 'f' Index does not exist , Therefore, the missing value introduced is NaN
print(s2)
# fill_value: Fill in the missing value 
s3 = s.reindex(list("qwert"),fill_value = 0)
print(s3)

alignment

import numpy
import pandas
# Series It will be automatically aligned according to the label 
s1 = pandas.Series(numpy.random.rand(3),index = ([" Love clothes "," Li Yi "," Meiqin "]))
s2 = pandas.Series(numpy.random.rand(2),index = ([" Meiqin "," Li Yi "]))
# index The order does not affect the numerical calculation , It will be calculated by label 
# Null value and any value calculation result is still null 
print(s1 + s2)

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