Python uses PyJanitor's aggregate and groupby_ Agg, pivot_ Long function for data aggregation

Preparation work: 1. Ensure that Python and related development environments are installed. 2. Install PyJanitor library: You can use the pip command to install and run 'pip install pyjanitor' from the command line. Dependent class libraries: -Pandas: used for data processing and analysis, PyJanitor is extended based on Pandas. Data sample: We use a sales dataset as an example data, including order number, product type, sales volume, and sales volume. The example data is as follows: |Order Number | Product Type | Sales Volume | Sales Volume| |-------|---------|-------|-------| |001 | A | 10 | 100| |002 | B | 5 | 50| |003 | A | 8 | 80| |004 | C | 3 | 30| |005 | B | 2 | 20| Full Python code: python import pandas as pd import janitor #Create Sample Data Box data = { Order number: ['001 ',' 002 ',' 003 ',' 004 ',' 005 '], 'Product Type': ['A ',' B ',' A ',' C ',' B '], 'Sales volume': [10, 5, 8, 3, 2], 'Sales': [100, 50, 80, 30, 20] } df = pd.DataFrame(data) #Aggregating data using the aggregate function summary = df.aggregate({ Sales Volume: 'sum', Sales revenue: 'mean' }) Print ("Use the aggregate function to aggregate data:") print(summary) #Using groupby_ AGG function aggregates data Grouped_ Summary=df. groupby_ Agg (by='Product Type', agg={'Sales Volume': 'sum', 'Sales Volume': 'mean'}) print(" Using groupby_ Agg function aggregates data: print(grouped_summary) #Using pivot_ Long function for data perspective Pickled_ Data=df.pivot_ Long (index='Order number', Column_ Names_ To='Indicator', Values_ To='value', Value_ Name_ Column='Indicator name') print(" Using pivot_ Long function for data pivot: print(pivoted_data) Code Interpretation: Firstly, import the required class libraries, including Pandas and Janitor. 2. Create an example data box df, including order number, product type, sales volume, and sales volume. 3. Use the 'df. aggregate()' function to aggregate data and calculate the total sales volume and the average sales volume. 4. Use 'df. groupby'_ The agg() function groups data by product type and performs aggregation operations to calculate the total sales volume and average sales volume for each product type. 5. Use ` df.pivot_ The longer() function converts the wide format data pivot to a long format, specifies the index column as the order number, converts the column name to a new indicator column, and uses the corresponding value as the value column, while adding a new indicator name column. 6. Print aggregated and perspective results. Summary: Python's PyJanitor library provides some convenient functions for aggregation and perspective operations on data. The 'aggregate()' function allows for convenient aggregation calculations of the entire data frame` Groupby_ The agg() function can perform grouping and aggregation operations by specified columns` Pivot_ The longer() function can pivot data from a wide format to a long format. These functions are very practical in data processing and analysis, and can improve the efficiency of data processing.