SoFunction
Updated on 2024-10-30

Analysis of the code example of Series in pandas

Series creation in pandas

Citation database

import pandas as pd
s=( data, index, dtype, copy,name)
Parameter name descriptive
data The input data, which can be lists, constants, ndarray arrays, and so on.
index The index value must be unique; if no index is passed, it defaults to (n).
dtype dtype indicates the data type, which is automatically determined if not provided.
copy Indicates a copy of the data, default is False.
name Receives either string or list. indicates the name of the Series object. Defaults to None

1) Create it with listlist in python:

import pandas as pd
my_list=[1,2,3,4]
my_Series=(my_list)
print(my_list)

The output is as follows:

0    1
1    2
2    3
3    4
dtype: int64

2) Create with numpy array

import pandas as pd
import numpy as np
my_array=([1,2,3,4])
s=(my_array)
print(s)

The output is as follows:

0    1
1    2
2    3
3    4
dtype: int32

3) Create a dictionary dict in python:

import pandas as pd
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = (data)
print(s)

The output is as follows:

a 0.0
b 1.0
c 2.0
dtype: float64

4) Scalar creation of Series objects:

If data is a scalar value, thenIndexing must be providedThe example is as follows:

import pandas as pd
s = (5, index=[0, 1, 2, 3])
print(s)

The output is as follows:

0  5
1  5
2  5
3  5
dtype: int64

5) Pass the index for the index parameter:

Take the dict method above as an example:

import pandas as pd
data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = (data,index=['b','c','d','a'])
print(s)

The output is as follows:

b 1.0
c 2.0
d NaN
a 0.0
dtype: float64

Note that the index returns a NAN value if there is no match.

Series access in pandas

# Two ways to access data in a Series
import pandas as pd
s1 = ([ 75, 90, 61],index=['Zhang San', 'Li Si', 'Chen Wu'])
print(s1[0])             # Accessed by element storage location
print(s1['Zhang San'])         # Accessed by specified index
# The results are all 75

1) Slicing operation

The concept of data slicing is derived from Numpy arrays, and the Series object uses data access methods similar to ndarray in NumPy to implement slicing operations.

Slicing operations for #Series
import pandas as pd
s1 = ([ 75, 90, 61, 59],index=['a', 'b', 'c', 'd']) 
s1[1:3]

2) Accessing Series data via bool arrays

import pandas as pd
import numpy as np
my_array=([1,2,3,4])
s=(my_array,['a','b','c','d'])
print(s)
print(s[>'a'])

Output:

a    1
b    2
c    3
d    4
dtype: int32
b    2
c    3
d    4
dtype: int32

3) Data modification

You can modify the corresponding value in the Series directly by assignment.

# Modify the values in the Series
import pandas as pd
s1 = ([ 75, 90, 61],index=['Zhang San', 'Li Si', 'Chen Wu'])
s1['Zhang San'] = 60         # Accessed by specified index
s1[1] = 60            # Accessed by element storage location
print(s1)

Output data:

Zhang San 60
Li Si 60
Chen V 61
dtype: int64

4) Arithmetic operations

Pandas will be based on the index index of the corresponding data for the calculation. As shown in the code, you can directly add, subtract, multiply or divide the Series structure, and output NaN when the index does not match.

import pandas as pd
sr1 = ([1, 2, 3, 4],['a','b','c','d'])
sr2 = ([1, 5, 8, 9],['a','c','e','f'])
print(sr2 - sr1)

Output:

a    0.0
b    NaN
c    2.0
d    NaN
e    NaN
f    NaN
dtype: float64

Common properties of Series in pandas

name (of a thing) causality
axes Returns all row index labels as a list.
dtype Returns the data type of the object.
empty Returns an empty Series object.
ndim Returns the dimension of the input data.
size Returns the number of elements of the input data.
values Returns the Series object as an ndarray.
index Returns a RangeIndex object that describes the range of values for the index.
  • values : Returns a Series object as an ndarray.
import pandas as pd
import numpy as np
s = ((6))
print(s)
print("Output data from series.")
print()

Output:

0   -0.502100
1    0.696194
2   -0.982063
3    0.416430
4   -1.384514
5    0.444303
dtype: float64
Outputting data from a series
[-0.50210028  0.69619407 -0.98206327  0.41642976 -1.38451433  0.44430257]

  • axes: return all row index labels as a list.
import pandas as pd
import numpy as np
s = ((5))
print ("The axes are:")
print()

Output:

The axes are:
[RangeIndex(start=0, stop=5, step=1)]

Series Common Methods

1) Statistical methods

2) Append Series and insert individual values

Similar to a list, the append method inserts (appends) a new series to the original series; if only a single value is to be inserted into the original series, the assignment method is sufficient.

import pandas as pd
list1 = [0, 1, 2, 3, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
series1 = ([4, 5], index = ['f', 'g'])
# Additional Series
print('After inserting series1 in series is: \n', (series1))

Output:

After inserting series1 in series for:
a    3
b    1
c    2
d    3
e    4
f    4
g    5
dtype: int64

3) Delete Series elements

import pandas as pd
list1 = [0, 1, 2, 3, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
('e', inplace = True)
print( series)

Output:

a    0
b    1
c    2
d    3
dtype: int64

4) Usage of unique() and nunique()

unique() returns the value of the de-duplicated element.

import pandas as pd
list1 = [0, 1, 2, 4, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
print(())

Output:

[0 1 2 4]

nunique() returns the number of elements after de-emphasis

import pandas as pd
list1 = [0, 1, 2, 4, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
print(())

Output:

4

value_counts(), the number of different elements.

import pandas as pd
list1 = [0, 1, 2, 4, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
print(series.value_counts())

Output:

4    2
0    1
1    1
2    1
dtype: int64

value_counts(), the number of different elements.

import pandas as pd
list1 = [0, 1, 2, 4, 4]
series = (list1, index = ['a', 'b', 'c', 'd', 'e'])
print(series.value_counts())

Output:

4    2
0    1
1    1
2    1
dtype: int64

to this article on pandas in the Series code example analysis of the article is introduced to this, more relevant pandas Series examples of content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!