使用Python对显示日期、类别、sup的数据进行分析。我的数据有三个字段,日期、类别和超级。我需要在其他两个领域看到超级模式。
# supe Pattern Analysis
# Import necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Step 1: Load Your Data`enter code here`
# Replace 'your_excel_file.xlsx' with your actual file path
file_path = 'your_excel_file.xlsx'
df = pd.read_excel(file_path)
# Preview the dataset
print("Dataset Head:")
print(df.head())
# Step 2: Preprocess the Data
# Convert Date to datetime format
df['Date'] = pd.to_datetime(df['Date'])
# Extract Year-Month for analysis
df['Month'] = df['Date'].dt.to_period('M')
# Check for missing values
print("\nMissing Values:")
print(df.isnull().sum())
# Step 3: Aggregate Data
# Group by Category, Month, and supe
grouped = df.groupby(['Category', 'Month', 'supe']).size().reset_index(name='Count')
# Preview the grouped data
print("\nGrouped Data:")
print(grouped.head())
# Step 4: Visualize Patterns
# Visualization 1: Heatmap of supe Counts Across Categories and Months
print("\nCreating Heatmap Visualization...")
# Pivot the data for heatmap
heatmap_data = grouped.pivot_table(index='Month', columns='Category', values='Count', aggfunc='sum', fill_value=0)
# Create the heatmap
plt.figure(figsize=(12, 8))
sns.heatmap(heatmap_data, cmap='coolwarm', annot=True, fmt='d')
plt.title('supe Patterns Across Categories and Months')
plt.show()
# Visualization 2: Line Plot to Show Trends Over Time
print("\nCreating Line Plot Visualization...")
# Example: Filter data for a specific supe
supe_name = 'supe_Name' # Replace with a supe's name or iterate through unique supes
supe_data = grouped[grouped['supe'] == supe_name]
# Line plot
plt.figure(figsize=(10, 6))
sns.lineplot(data=supe_data, x='Month', y='Count', hue='Category')
plt.title(f'Trend of Activities for {supe_name}')
plt.xlabel('Month')
plt.ylabel('Count')
plt.xticks(rotation=45)
plt.show()