pandas contains a DataFrame and Series data types, respectively, two-dimensional data structures and one-dimensional data structures.
Simply can be understood as Series for excel table of a row or column, DataFrame is a multi-row multi-column area.
Series Type
- When we say excel in a column segment of the data (a separate column), say the first few data, we will generally say, is the first few rows of data, then, visible although it is a one-dimensional data, but there is an index.
- The default index for Series data is 0,1,2,3,4,5..., also known as position index or implicit index. After customizing the index, which is called labeled index, you can access Series with positional index and label.
Three ways to create a Series
Creating a Series from an Array
import pandas as pd import numpy as np s1 = ([1,2,3,'tom',True]) s2 = (range(0, 10, 1)) print(s1) print(s2) print(type(s1), type(s2))
Creating a Series with Specified Index Columns
Index as an array
s1 = ([1,2], index=["a", "b"]) s2 = (range(10,15,1), index=list('ngjur')) s3 = (range(100,110,2), index=range(4,9,1)) print(s1) print(s2) print(s3) print(s1["a"], s1[1]) # Position index starts at 0 print(s2["r"], s2[-2]) The # position index starts at 0 and can be accessed with the same index as a list, -1 means the last element print(s3[4]) # When the defined index is a number, it will overwrite the previous positional index in a way that means s3[0] to s3[3], s3[-1] will no longer be accessible.
a 1
b 2
dtype: int64
n 10
g 11
j 12
u 13
r 14
dtype: int64
4 100
5 102
6 104
7 106
8 108
dtype: int64
1 2
14 13
100
Using Dictionaries to Create
key is the label index, value is the value of each element of the series
s1 = ({'tom':'001', 'jack':'002'}) print(s1)
tom 001
jack 002
dtype: object
Scalar Creates Series Objects
If data is a scalar value, an index must be provided
s1 = (5, [0, 1, 2, "a"]) print(s1[[1, "a"]])
1 5
a 5
dtype: int64
Common Operations of Series
Series value access
series_name[],[] can be a single position index or label index, a position slice or label slice, a list of position indices or a list of label indices
s1 = ({'tom':'001', 'jack':'002', "Jim":"003"}) s2 = s1[["tom", "jack"]] #Using tagged indexed lists s3 = s1[0:3] # Use location slicing s4 = s1["tom":"Jim"] #Using tag slicing s5 = s1[[0,1]] print("s1-----\n", s1["tom"], type(s1[1])) print("s2-----\n", s2, type(s2)) #Using tagged indexed lists print("s3-----\n", s3, type(s3)) #Use location slicing print("s4-----\n", s4, type(s4)) #Using tag slicing print("s5-----\n", s5, type(s5)) #Use the location index list
s1-----
001 <class 'str'>
s2-----
tom 001
jack 002
dtype: object <class ''>
s3-----
tom 001
jack 002
Jim 003
dtype: object <class ''>
s4-----
tom 001
jack 002
Jim 003
dtype: object <class ''>
s5-----
tom 001
jack 002
dtype: object <class ''>
Access to the entire series
- series_name.values property
- Return Type
s1 = ({'tom':'001', 'jack':'002', "Jim":"003"}) s2 = print("s2-----\n", s2, type(s2)) s3 = ({'tom':90, 'jack':40, "Jim":100})
s2-----
['001' '002' '003'] <class ''>
s2-----
[ 90 40 100] <class ''>
Get indexed columns
series_name.index s1 = (['tom', 'jack', "Jim"], [90, 100, 60]) print("s1-----\n", s1, type(s1)) s1_index = print("s1_index-----\n", s1_index, type(s1_index)) print("s1_name:", )
s1-----
90 tom
100 jack
60 Jim
dtype: object <class ''>
s1_index-----
Int64Index([90, 100, 60], dtype='int64') <class '.Int64Index'>
s1_name----- None
Setting the name
If Series is used to generate a DataFrame, the name of the Series will be its index or column name.
s1 = ((5), name='ABC',index=['a','b','c','d','e']) print(s1)
a 0
b 1
c 2
d 3
e 4
Name: ABC, dtype: int32
Series data editing
Series Data Deletion
Use series_name.drop() to specify the index, which can be a label index, or a list of multiple label indexes, not a position index, or a slice.
Series Data Deletion
drop method
s1 = ((5), name='A',index=['a','b','c','d','e']) print(s1) # Single-value deletion, specify label index ('c',inplace=False) #inplace to False does not change the contents of the original s1 print("Delete individual values without changing s1:\n",s1) # Multiple value deletion, specifying a list of labeled indexes (['c','e'],inplace=False)
a 0
b 1
c 2
d 3
e 4
Name: A, dtype: int32
Delete a single value without changing s1.
a 0
b 1
c 2
d 3
e 4
Name: A, dtype: int32a 0
b 1
d 3
Name: A, dtype: int32
# multiindex value deletion midx = (levels=[['lama', 'cow', 'falcon'], ['speed', 'weight', 'length']], codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2], [0, 1, 2, 0, 1, 2, 0, 1, 2]]) s1 = ([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3], index=midx) print(s1) (labels='weight', level=1)
lama speed 45.0
weight 200.0
length 1.2
cow speed 30.0
weight 250.0
length 1.5
falcon speed 320.0
weight 1.0
length 0.3
dtype: float64
lama speed 45.0
length 1.2
cow speed 30.0
length 1.5
falcon speed 320.0
length 0.3
dtype: float64
pop method
pop(x), specifies the index of the label to be popped
s1 = ([1, 2, 3], index=["a", "b", "c"]) ("a") print(s1)
b 2
c 3
dtype: int64
del method
del s1[x], Specify the index of the label to be deleted. s1 = ([1, 2, 3], index=["a", "b", "c"]) del s1["a"] print(s1)
b 2
c 3
dtype: int64
Series Data Addition
Similar to how elements are added in a dictionary
s1 = ([1, 2, 3], index=["a", "b", "c"]) s1["d"] = 4 print(s1)
a 1
b 2
c 3
d 4
dtype: int64
append method
- The Pandas () function is used to join two or more series of objects, the original object does not change, this is different from a list.
- (to_append, ignore_index=False, verify_integrity=False)
- to_append: series or series list/tuple
- ignore_indexd: If True, do not use index tags If True, raise an exception when creating indexes with duplicate entries.
s1 =(["Beijing.", "Shanghai.", "*.", "*"]) index_list =["a", "b", "c", "d"] = index_list print("s1-----------\n", s1) s2 = ({"e": "Guangzhou.", "f": "Shenzhen"}) print("s2-----------\n", s2) s3 = (s2) print("s3-----------\n", s3) print(s1) s4 = (s2, ignore_index=True) print("s4-----------\n", s4)
s1-----------
a Beijing
b Shanghai
c *
d *
dtype: object
s2-----------
e Guangzhou
f Shenzhen
dtype: object
s3-----------
a Beijing
b Shanghai
c *
d *
e Guangzhou
f Shenzhen
dtype: object
a Beijing
b Shanghai
c *
d *
dtype: object
s4-----------
0 Beijing
1 Shanghai
2 *
3 *
4 Guangzhou
5 Shenzhen
dtype: object
to this article on the specific use of pandas data types Series article is introduced to this, more related pandas Series content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!