- Read the CSV file: Use pd.read_csv to load the CSV file into a pandas DataFrame.
- Define Age Groups by creating a dictionary containing age group conditions using Boolean conditions.
- Segment Visitors by iterating through the dictionary and filter the visitors into respective age groups.
- Visualize the result using matplotlib.
import pandas as pd
import matplotlib.pyplot as plt
# Load the dataset
visitors_df = pd.read_csv("clustervisitor.csv") # Insert the file path to your CSV file
# Define age groups
age_groups = {
'Young': (visitors_df['Age'] <= 30),
'Middle-aged': ((visitors_df['Age'] > 30) & (visitors_df['Age'] <= 50)),
'Elderly': (visitors_df['Age'] > 50)
}
# Print visitors in each age group
for group, condition in age_groups.items():
visitors_in_group = visitors_df[condition]
print(f"Visitors in {group} age group:")
print(visitors_in_group)
print()
# Count visitors in each age group
visitors_counts = []
for group, condition in age_groups.items():
visitors_in_group = visitors_df[condition]
visitors_counts.append(len(visitors_in_group))
# Plot the distribution of visitors across age groups
age_group_labels = list(age_groups.keys())
plt.figure(figsize=(8, 6))
plt.bar(age_group_labels, visitors_counts, color='skyblue')
plt.xlabel('Age Groups')
plt.ylabel('Number of Visitors')
plt.title('Visitor Distribution Across Age Groups')
plt.show()
Thus the Implementation of Cluster and Visitor Segmentation for Navigation patterns is executed successfully.