- 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.
# read the data
import pandas as pd
visitor_df = pd.read_csv('/content/clustervisitor.csv')
# Perform segmentation based on characteristics (e.g., age groups)
age_groups = {
'Young': visitor_df['Age'] <= 30,
'Middle-aged': (visitor_df['Age'] > 30) & (visitor_df['Age'] <= 50),
'Elderly': visitor_df['Age'] > 50
}
for group, condition in age_groups.items():
visitors_in_group = visitor_df[condition]
print(f"Visitors in {group} age group:")
print(visitors_in_group)
# Create a list to store counts of visitors in each age group
visitor_counts=[]
# Count visitors in each age group
for group,condition in age_groups.items():
visitors_in_group=visitor_df[condition]
visitor_counts.append(len(visitors_in_group))
# Define age group labels and plot a bar chart
age_group_labels=list(age_groups.keys())
plt.figure(figsize=(8, 6))
plt.bar(age_group_labels, visitor_counts, color='skyblue')
plt.xlabel('Age Groups')
plt.ylabel('Number of Visitors')
plt.title('Visitor Distribution Across Age Groups')
plt.show()
Thus the cluster and visitor segmentation for navigation patterns was implemented successfully in python.