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
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
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'])
2. Dictionary type creation
import pandas as pd
d = pd.Series({
'a':9,'b':8,'c':7})
import pandas as pd
c = pd.Series({
'a':9,'b':8,'c':7},
index=('c','a','b','d'))
c
3.ndarray creation
import pandas as pd
import numpy as np
n = pd.Series(np.arange(5))
n
import pandas as pd
import numpy as np
m=pd.Series(np.arange(5),
index=np.arange(9,4,-1))
m
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)
Automatic index and custom index coexist. Although the two sets of indexes coexist, they cannot be mixed.
1. After operating on the Series type, it is still the Series type. But when only one value is queried, one value is returned.
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.
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)
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)
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)
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)