Example codes for comparing SPL, SQL, and Python

 

Sepcification

Since most Python codes will use the pandas and numpy packages, any Python code in this article imports the following packages at the beginning by default:

import pandas as pd
import numpy as np

Contents

1 Data object

1.1 Simple set
1.2 Structured data

2 Loop function

2.1 Current value reference
2.2 Using variables
2.3 Sequence number reference
2.4 Adjacent reference
2.5 Loop to generate and execute

3 Grouping operations

3.1 Equivalence grouping
3.2 Non-equivalence grouping
3.3 Ordered grouping
3.4 Inverse grouping

4 Aggregation operations

4.1 Basic aggregation
4.2 Selection as aggregation
4.3 Set aggregation
4.4 Group & aggregate

5 Iterative operations

5.1 Simple iterative operations
5.2 Early terminated iterative operation
5.3 Iteration in groups

6 Position and select

6.1 Positioning operations
6.2 Selection operation
6.3 Positioning calculation

7 Sort and transpose

7.1 Sort a sequence

7.2 Sort the structured data

7.3 Rank a sequence

7.4 Rank the structured data

7.5 Transpose

8 Join operations

8.1 Foreign key association

8.2 Primary key association

8.3 Mixed association

9 Comprehensive examples

9.1 If a user does not take any action within 10 minutes or does not log in within 5 minutes after logging out, the session is considered over. Calculate the number of sessions for each user

9.2 Count the player who scores 3 times in a row within 1 minute

9.3 Calculate the number of users who are active for three consecutive days in every 7 days

9.4 Calculate the next-day retention rate of new users per day

9.5 Calculate the increase of stock price on the day when it is higher than those on the previous and next 5 days