Series type of Pandas library

Series type consists of a groupdataAnd related data Index (automatic index, can also be set) composition.

Example 1 (list creation Series type):
import pandas as pd
a = pd.Series([9,8,7,6])
a

Insert picture description here

Example 2:

If the index position is the second place,'index=' can be omitted

import pandas as pd
b = pd.Series([9,8,7,6],
		index=['a','b','c','d'])
b

Insert picture description here
Series types can be created by the following types:

  • Python list
  • Scalar value
  • Python dictionary
  • ndarray
  • Other functions (range() function)
1. Scalar value creation

Cannot omit'index='

import pandas as pd
s = pd.Series(25,index=['a','b','c'])

Insert picture description here

2. Dictionary type creation
import pandas as pd
d = pd.Series({
    
    'a':9,'b':8,'c':7})

Insert picture description here

import pandas as pd
c = pd.Series({
    
    'a':9,'b':8,'c':7},
		index=('c','a','b','d'))
c

Insert picture description here

3.ndarray creation
import pandas as pd
import numpy as np
n = pd.Series(np.arange(5))
n

Insert picture description here

import pandas as pd
import numpy as np
m=pd.Series(np.arange(5),
	index=np.arange(9,4,-1))
m

Insert picture description here

Basic operation of Series type

Similar to ndarray and python dictionary operations, value and index can be manipulated.

import pandas as pd
b = pd.Series([9,8,7,6],
              ['a','b','c','d'])
print(b)
print('b.index:\n', b.index)
print('b.values:\n', b.values)

Insert picture description here
Automatic index and custom index coexist. Although the two sets of indexes coexist, they cannot be mixed.
Insert picture description here
1. After operating on the Series type, it is still the Series type. But when only one value is queried, one value is returned.
Insert picture description here
2. Similar to dictionary operations, the in, and get methods are retained. in is to query whether a certain key is in it,And in can only judge the automatic index, not the custom index. In the Series type, it is to determine whether an index is in it. get is to extract the value corresponding to an index.
Insert picture description here
3.Series alignment operation.
Perform calculations with the same index value, and the final number of elements is the union of all elements.

import pandas as pd
a = pd.Series([1,2,3],['c','d','e'])
b = pd.Series([9,8,7,6],
              ['a','b','c','d'])
print(a+b)

Insert picture description here
4.Series type name attribute

import pandas as pd
b = pd.Series([9,8,7,6],
              ['a','b','c','d'])
b.name = 'Series对象'
b.index.name = '索引列'
print(b)

Insert picture description here
5.
Series type modification Series objects can be modified at any time and take effect immediately

import pandas as pd
b = pd.Series([9,8,7,6],
              ['a','b','c','d'])
b['a']=15
b.name = 'Series'
print(b)

Insert picture description here

import pandas as pd
b = pd.Series([9,8,7,6],
              ['a','b','c','d'])
b['b','c'] = 20
b.name = 'New Series'
print(b)

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_42253964/article/details/106921447