In Python's Pandas library,Series
Objects support a variety of operations, including arithmetic operations, comparison operations, and logical operations. Below, I will demonstrate code examples of these operations separately and provide corresponding scenario descriptions.
1. Arithmetic operations
Arithmetic operations include basic operations such as addition, subtraction, multiplication, and division. Pandas allows these operations on Series while automatically aligning the indexes between different Series.
When the Series length is the same
Sample code:
import pandas as pd # Create two Series objectss1 = ([1, 2, 3], index=['a', 'b', 'c']) s2 = ([4, 5, 6], index=['a', 'b', 'c']) # Addition operationresult_add = s1 + s2 print("Addition result:\n", result_add) # Subtraction operationresult_sub = s1 - s2 print("Subtraction result:\n", result_sub) # Multiplication operationresult_mul = s1 * s2 print("Multiple result:\n", result_mul) # Division operationresult_div = s1 / s2 print("Divide result:\n", result_div)
Execution results:
Addition result:
a 5
b 7
c 9
dtype: int64
Subtraction results:
a -3
b -3
c -3
dtype: int64
Multiplication result:
a 4
b 10
c 18
dtype: int64
Division results:
a 0.25
b 0.40
c 0.50
dtype: float64
Applicable scenarios:
When performing statistical analysis or data preprocessing, it can be used to calculate the sum, difference, product or merchandise of different data, such as calculating total sales or average sales.
Series lengths are different
When the index does not correspond completely, the index of the result will be a union of two Series indexes, and the non-existent index will be filled withNaN
。
Sample code:
import pandas as pd # Create two Series of different lengthss1 = ([1, 2, 3], index=['a', 'b', 'c']) s2 = ([4, 5, 6, 7], index=['b', 'c', 'd', 'e']) # Addition operationresult_add = s1 + s2 print("Addition result:\n", result_add) # Multiplication operationresult_mul = s1 * s2 print("Multiple result:\n", result_mul)
Execution results:
Addition result:
a NaN
b 6.0
c 8.0
d NaN
e NaN
dtype: float64Multiplication result:
a NaN
b 8.0
c 15.0
d NaN
e NaN
dtype: float64
Applicable scenarios:
It is very suitable for time series data in financial data analysis, because different financial instruments may have transaction records at different times, and in this way, the problem of data alignment can be easily handled.
2. Comparison operation
Comparison operations include equal to, not equal to, greater than, less than, etc., and are used to compare elements in Series.
When the Series length is the same
Sample code:
# Comparison operationresult_gt = s1 > s2 print("Greater than the result of the calculation:\n", result_gt) result_eq = s1 == s2 print("Equal to the calculation result:\n", result_eq)
Execution results:
Greater than the calculation result:
a False
b False
c False
dtype: bool
Equal to the calculation result:
a False
b False
c False
dtype: bool
Applicable scenarios:
In the data filtering process, comparison operations are often used to filter data based on conditions, such as filtering out all records whose sales volume exceeds a certain threshold.
Series lengths are different
Comparison operations (equal to, not equal to, greater than, less than, etc.) will also produceNaN
。
Sample code:
# equals operationresult_eq = s1 == s2 print("Equal to the calculation result:\n", result_eq)
Execution results:
Equal to the calculation result:
a False
b False
c False
d False
e False
dtype: bool
Applicable scenarios:
The same applies to data alignment and comparison of time series. For example, it is used to compare whether stock prices at different points in time are equal.
3. Logical operations
When the Series length is the same
Logical operations mainly perform and, or, and not operations on the bool values in Series.
Sample code:
# Create a series of logical operationss3 = ([True, False, True]) s4 = ([False, True, True]) #Logistics and operationsresult_and = s3 & s4 print("With the result of the calculation:\n", result_and) # Logic or operationresult_or = s3 | s4 print("Or operation result:\n", result_or)
Execution results:
With the calculation result:
0 False
1 False
2 True
dtype: bool
Or calculation result:
0 True
1 True
2 True
dtype: bool
Applicable scenarios:
In the case of processing multiple conditional filtering, for example, data filtering processing that satisfies multiple conditions simultaneously or at least one condition is satisfied.
Series lengths are different
Logical operations (As, or, non) will appearNaN
, because Boolean logic operations are involvedNaN
The result is alsoNaN
。
Sample code:
# Create logical data seriess3 = ([True, False, True], index=['a', 'b', 'c']) s4 = ([False, True, True, False], index=['b', 'c', 'd', 'e']) #Logistics and operationsresult_and = s3 & s4 print("With the result of the calculation:\n", result_and) # Logic or operationresult_or = s3 | s4 print("Or operation result:\n", result_or)
Execution results:
With the calculation result:
a False
b False
c True
d False
e False
dtype: boolOr calculation result:
a True
b True
c Trued True
e False
dtype: bool
Applicable scenarios:
Logical operations are usually used to process data filtering. In actual data processing, for example, when processing user behavior data, it may be necessary to determine the user's final behavioral tendency based on the behavior data at multiple points in time, and logical operations can be used to combine conditions at different points in time.
Summarize
For different lengthsSeries
When performing calculations, Pandas' processing method is very intelligent, it uses it automatically aligned indexes and usedNaN
Filling in missing values ensures the feasibility of the calculation and the accuracy of the results. This makes Pandas particularly powerful and flexible in handling irregular data encountered in actual work.
- In Financial AnalysisIn this article, it is often necessary to align trading data, such as daily trading data of stocks, especially when combining multiple stock data for comparison.
- Processing of scientific research dataIn this case, such as bioinformatics or meteorological data analysis, the time points of the data may not be completely consistent, and this alignment is extremely important at this time.
- In business intelligenceIn this article, when processing sales data or user behavior data, it is necessary to integrate and analyze data at different stages of the product line.
Pandas greatly simplifies the complexity of data preprocessing through this flexible data processing method, allowing data analysts to focus more on data analysis itself rather than spending a lot of time dealing with data alignment and missing issues.
This is the article about Series operations summary (arithm, comparison and logical operations) in pandas. For more related pandas Series operations, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!